subquery in SQL

A subquery is a query that is nested inside a SELECT , INSERT , UPDATE , or DELETE statement, or inside another subquery. ... A subquery can be used anywhere an expression is allowed. In this example a subquery is used as a column expression named MaxUnitPrice in a SELECT statement

副問い合わせとは、SELECT、INSERT、UPDATE、DELETE文の中、または別の副問い合わせの中に入れ子になっている問い合わせのことです。... 副問い合わせは、式を使用できる場所であればどこでも使用することができます。この例では、副問い合わせはSELECT文のMaxUnitPriceという列式として使用されています。

What are sub queries in SQL?

A Subquery or Inner query or a Nested query is a query within another SQL query and embedded within the WHERE clause. A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.

サブクエリ、インナークエリ、またはネストされたクエリは、他のSQLクエリの中にあるクエリで、WHERE句の中に埋め込まれています。サブクエリは、メインクエリで使用されるデータを返すために使用され、取得するデータをさらに制限するための条件として使用されます。

Why subquery is used?

A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved. Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN

副問い合わせは、取得するデータをさらに制限する条件として、メインの問い合わせで使用されるデータを返すために使用されます。副問い合わせは、SELECT、INSERT、UPDATE、DELETEの各文と、=、<、>、>=、<=、IN、BETWEEN、<=の各演算子で使用できます。

Why do we use subquery?


A Subquery or Inner query or Nested query is a query within SQL query and embedded within the WHERE clause. A Subquery is a SELECT statement that is embedded in a clause of another SQL statement. They can be very useful to select rows from a table with a condition that depends on the data in the same or another table.

なぜ副問い合わせを使うのか?
サブクエリ、インナークエリ、ネストされたクエリは、SQLクエリの中のクエリで、WHERE句の中に埋め込まれています。副問い合わせとは、他のSQL文の節の中に埋め込まれたSELECT文のことです。副問い合わせは、同じテーブルまたは別のテーブルのデータに依存する条件でテーブルから行を選択するのに非常に役立ちます。

Why use subqueries instead of joins?


Subqueries can be used to return either a scalar (single) value or a row set; whereas, joins are used to return rows. A common use for a subquery may be to calculate a summary value for use in a query. For instance we can use a subquery to help us obtain all products have a greater than average product price.

なぜ結合ではなくサブクエリを使うのですか?
副問い合わせは、スカラー(単一)の値または行セットを返すために使用できます。副問い合わせの一般的な使用法は、問い合わせで使用するための要約値を計算することです。例えば、製品価格が平均よりも高い製品をすべて取得するために副問い合わせを使用することができます。

Is SQL a query?


A query is a question or inquiry about a set of data. We use Structured Query Language (SQL) to retrieve meaningful and relevant information from databases. When building a structure, we pull data from tables and fields. The fields are columns in the database table, while the actual data makes up the rows

SQLはクエリですか?
クエリとは、データの集合に対する質問や問い合わせのことです。私たちは、データベースから意味のある適切な情報を取り出すために、SQL(Structured Query Language)を使用します。構造を構築する際には、テーブルとフィールドからデータを引き出します。フィールドはデータベーステーブルの列であり、実際のデータは行を構成します。

Is insert a query?


The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.

insert はクエリですか?
SQLのINSERT INTOステートメントは、データベースのテーブルにデータの新しい行を追加するために使用されます。

Is SQL a good career?


SQL is one of the most widely used programming languages in the world, and if you learn SQL, you open up some clear paths to finding new work and employment opportunities. ... The trend is clear: whether you're a product manager, a business analyst, an MBA, or a developer — SQL will upskill your career.

SQLは良いキャリアですか?
SQLは世界で最も広く使用されているプログラミング言語の一つであり、SQLを学べば、新しい仕事や雇用機会を見つけるための明確な道が開けます。... その傾向は明らかです。プロダクトマネージャー、ビジネスアナリスト、MBA、開発者のいずれであっても、SQLはあなたのキャリアをアップさせます。

How subquery works in SQL?


A subquery is a query within another query. The outer query is called as main query and inner query is called as subquery. The subquery generally executes first, and its output is used to complete the query condition for the main or outer query. Subquery must be enclosed in parentheses.

SQLでのサブクエリの動作は?
副問い合わせとは、他の問い合わせの中にある問い合わせのことです。外側のクエリをメインクエリと呼び、内側のクエリをサブクエリと呼びます。通常、副問い合わせは最初に実行され、その出力はメインまたは外側の問い合わせの条件を満たすために使用されます。サブクエリは括弧で囲む必要があります。


Subqueries in the WHERE Clause

A subquery in a WHERE clause can be used to qualify a column against a set of rows. For example, the following subquery returns the department numbers for departments on the third floor. The outer query retrieves the names of employees who work on the third floor.

WHERE句の副問い合わせは、一連の行に対して列を修飾するために使用できます。例えば、次の副問い合わせは、3階の部署の部署番号を返します。外部クエリは、3階で働く従業員の名前を取得します。
SELECT ename
FROM employee
WHERE dept IN
      (SELECT dno
      FROM dept
      WHERE floor = 3);

where句のサブクエリ


where句で判定する条件をレコードの問い合わせ結果を使う場合に利用します。

select (列名) 
from (テーブル名) 
where (条件式列名) > (select (関数等) from (テーブル名))
 SELECT value,end_date 
FROM avg_speed 
WHERE value = ( SELECT max(value) FROM avg_speed WHERE end_date < ? ) limit 2 ​







この記事が気に入ったらサポートをしてみませんか?