/

/

How to write SQL Subqueries - 3 Types of Subqueries

HOW TO

How to write SQL Subqueries - 3 Types of Subqueries

How to write SQL Subqueries - 3 Types of Subqueries

How to write SQL Subqueries - 3 Types of Subqueries

May 4, 2024

May 4, 2024

May 4, 2024

Dealing with databases and queries, but got tripped up with this thing called Subqueries? Don't worry; you're not alone, and I am going to help demystify it.

Imagine you are working on some database for your project, and you come across this concept - Subqueries. You see a big headache coming your way. But the good news is, I was there once and found that once broken down, subqueries aren't as daunting as they seem.

A subquery, essentially, is a query that's nestled within a larger query that's often referred to as the "main query". In a typical setup, you'd have two queries - the main one and a second one, nestled within brackets denoting it's a subquery. For example, if you had a query filtering employee names based on their salaries, the main query would be filtering the employee names and the subquery, nestled within brackets, would be where you'd specify the salary range.

You can use subqueries in a series of commands such as select, joins, and where clauses. They also come in handy when updating your databases using insert, update, or delete commands.

To understand the concept better, let's break down some terminologies:

  • A "subquery" is simply a query that's nestled within another query.

  • An "inner query" isolates the where or has clause within a query.

  • A "nested query" is a subquery that's nestled within another subquery.

Subqueries generally fall into three categories: scalar, multi-row, and correlated subqueries.

Let's unpack each type and learn how to write SQL subqueries.


Write a SQL Scalar Subquery

Known as a single row subquery, it yields a single value. It's useful when you anticipate needing one value for a select command or comparison operations with the where or having clause. We utilize aggregate functions that derive a singular result for this kind of subquery. For example, you might want to find employees who earn more than the average salary. Let’s write scalar subquery and you could execute the following SQL:

SELECT employee_name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);

The section in brackets represents our scalar subquery. It computes the average salary for all employees.


Write a SQL Multiple-row Subquery

This type returns multiple rows within a single column. This subquery type is useful when you need to cross verify or compare a list of values offered by a subquery. Using the same example, if you want to spot all employees in the same department as 'John', you'd use this handy SQL:

SELECT employee_name FROM employees WHERE department_id IN (SELECT department_id FROM employees WHERE employee_name = 'John');


Write SQL Correlated Subqueries

These are reliant on data from the outer query. The operation of a correlated subquery is dependent on the data used in the main query. This might be helpful for finding all employees whose salary exceeds the average in their department. Such information can be obtained using this SQL:

SELECT e1.employee_name, e1.salary FROM employees e1 WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e1.department_id = e2.department_id);

Correlated subqueries might run more slowly because of their multiple executions.

Subqueries may initially seem complex, but breaking them down into Scalar, Multirow, and Correlated subqueries makes them much more approachable. These are powerful tools that, when used correctly, can simplify your SQL queries and make your workflow more efficient. Remember, a subquery is essentially just a query within another query—nothing more, nothing less.

Ready to simplify your subquery experience? AI2SQL can make the process a whole lot smoother. Given it a shot today and see how it can revolutionize your SQL query building. Let us know how it has improved your workflow!

Share this

More Articles

More Articles

More Articles