Sqlite Subqueries

SQLite: Subqueries

This SQLite post explains how to use subqueries in SQLite with syntax and examples.

What is a subquery in SQLite?

In SQLite, a subquery is a query within a query. You can create subqueries within your SQL statements. These subqueries can reside in the WHERE clause, the FROM clause, or the SELECT clause.

Note

  • In SQLite, a subquery is also called an INNER QUERY or INNER SELECT.
  • In SQLite, the main query that contains the subquery is also called the OUTER QUERY or OUTER SELECT.

WHERE clause

Most often, the subquery will be found in the WHERE clause. These subqueries are also called nested subqueries.

For example:

SELECT e.employee_id, e.last_name
FROM employees e
WHERE e.department_id IN
   (SELECT d.department_id
    FROM departments d
    WHERE d.department_name = 'HR'
    OR d.department_name = 'Accounting');

The subquery portion of the SELECT statement above is:

(SELECT d.department_id
 FROM departments d
 WHERE d.department_name = 'HR'
 OR d.department_name = 'Accounting');

This subquery allows you to find all department_id values from the departments table that have an department_name that is either 'HR' or 'Accounting'. The subquery is then used to filter the results from the main query using the IN condition.

This subquery could have alternatively been written as an INNER join as follows:

SELECT e.employee_id, e.last_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id
WHERE d.department_name = 'HR'
OR d.department_name = 'Accounting';

This INNER JOIN would run more efficiently than the original subquery. It is important to note, though, that not all subqueries can be rewritten using joins.

FROM clause

A subquery can also be found in the FROM clause. These are called inline views.

For example:

SELECT departments.department_name, subquery1.latest_hire
FROM departments,
 (SELECT department_id, MAX(hire_date) AS latest_hire
  FROM employees
  GROUP BY department_id) subquery1
WHERE subquery1.department_id = departments.department_id;

In this example, we've created a subquery in the FROM clause as follows:

(SELECT department_id, MAX(hire_date) AS latest_hire
 FROM employees
 GROUP BY department_id) subquery1

This subquery has been aliased with the name subquery1. This will be the name used to reference this subquery or any of its fields.

SELECT clause

A subquery can also be found in the SELECT clause. These are generally used when you wish to retrieve a calculation using an aggregate function such as the sum, count, min, max , or avg function, but you do not want the aggregate function to apply to the main query.

For example:

SELECT e1.department_id,
  (SELECT MAX(hire_date)
   FROM employees e2
   WHERE e1.department_id = e2.department_id) subquery2
FROM employees e1;

In this example, we've created a subquery in the SELECT clause as follows:

(SELECT MAX(hire_date)
 FROM employees e2
 WHERE e1.department_id = e2.department_id) subquery2

The subquery has been aliased with the name subquery2. This will be the name used to reference this subquery or any of its fields.

The trick to placing a subquery in the SELECT clause is that the subquery must return a single value. This is why an aggregate function such as the sum, count, min, max, or avg function is commonly used in the subquery.