SQL ENTRY AND PREPARING SQL QUERIES

Subqueries

A subquery, also known as an inner query, is a structure used in SQL queries. A subquery is a query that resides within a main query and returns one or more records. Some problems can be solved using both joins and subqueries. In terms of performance, it would be more appropriate to choose subqueries in these situations. Subqueries are written between parentheses. The innermost subquery is executed first and its result is passed to the outer query.

A subquery, also known as an inner query, is a structure used in SQL queries. A subquery is a query that resides within a main query and returns one or more records. Some problems can be solved using both joins and subqueries. In terms of performance, it would be more appropriate to choose subqueries in these situations. Subqueries are written between parentheses. The innermost subquery is executed first and its result is passed to the outer query.

Subqueries are often used in the WHERE, HAVING, or FROM clauses of Select queries, as well as the INTO clause of Insert queries, the SET or WHERE clauses of Update queries, and the FROM or WHERE clauses of Delete queries.

Using a subquery within the WHERE condition of the main query, conditions of the main query can be determined based on the results returned by the subquery.

Most of the time, subqueries are used on the right side of comparison operators. The comparison operator changes depending on the number of results returned by the subquery. Additionally, subqueries cannot be used in the ORDER BY clause.

For example, let's say there is an orders table where orders and their total amounts are recorded. To find customers whose total order amount exceeds 500, a subquery can be used:

SELECT customer_name FROM customersWHERE customer_id IN ( SELECT customer_id  FROM orders  GROUP BY customer_id  HAVING SUM(order_total) > 500 )

In the solution above, the bolded part is the subquery.

Using a subquery in the FROM section of the main query, the subquery can be executed before selecting data from a table. For instance, a subquery can be used to determine the total order count of each customer in the orders table.

SELECT customer_name, order_countFROM ( SELECT customer_id, COUNT(*) AS order_count  FROM orders  GROUP BY customer_id)  AS order_countsJOIN customers ON order_counts.customer_id = customers.customer_id

In the solution above, the bolded part is the subquery. In this query, a subquery that finds the number of orders from the "orders" table for each customer is used. The result of this subquery is named as the "order_counts" table and it's joined with the "customers" table. As a result, the name and order count of each customer is displayed.

In the next learning content, we will look together at the grouping and summarization of data in tables.

A subquery, also known as an inner query, is a structure used in SQL queries. A subquery is a query that resides within a main query and returns one or more records. Some problems can be solved using both joins and subqueries. In terms of performance, it would be more appropriate to choose subqueries in these situations. Subqueries are written between parentheses. The innermost subquery is executed first and its result is passed to the outer query.

A subquery, also known as an inner query, is a structure used in SQL queries. A subquery is a query that resides within a main query and returns one or more records. Some problems can be solved using both joins and subqueries. In terms of performance, it would be more appropriate to choose subqueries in these situations. Subqueries are written between parentheses. The innermost subquery is executed first and its result is passed to the outer query.

Subqueries are often used in the WHERE, HAVING, or FROM clauses of Select queries, as well as the INTO clause of Insert queries, the SET or WHERE clauses of Update queries, and the FROM or WHERE clauses of Delete queries.

Using a subquery within the WHERE condition of the main query, conditions of the main query can be determined based on the results returned by the subquery.

Most of the time, subqueries are used on the right side of comparison operators. The comparison operator changes depending on the number of results returned by the subquery. Additionally, subqueries cannot be used in the ORDER BY clause.

For example, let's say there is an orders table where orders and their total amounts are recorded. To find customers whose total order amount exceeds 500, a subquery can be used:

SELECT customer_name FROM customersWHERE customer_id IN ( SELECT customer_id  FROM orders  GROUP BY customer_id  HAVING SUM(order_total) > 500 )

In the solution above, the bolded part is the subquery.

Using a subquery in the FROM section of the main query, the subquery can be executed before selecting data from a table. For instance, a subquery can be used to determine the total order count of each customer in the orders table.

SELECT customer_name, order_countFROM ( SELECT customer_id, COUNT(*) AS order_count  FROM orders  GROUP BY customer_id)  AS order_countsJOIN customers ON order_counts.customer_id = customers.customer_id

In the solution above, the bolded part is the subquery. In this query, a subquery that finds the number of orders from the "orders" table for each customer is used. The result of this subquery is named as the "order_counts" table and it's joined with the "customers" table. As a result, the name and order count of each customer is displayed.

In the next learning content, we will look together at the grouping and summarization of data in tables.

Yardımcı kaynaklar

Glossary