SQLite Subqueries

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

SQLite Subqueries

Introduction

A subquery is a SQL query nested inside a larger query.

  • A subquery may occur in : 
     
    • - A SELECT clause
    • - A FROM clause
    • - A WHERE clause
       
  • A subquery is usually added within the WHERE Clause of another SQL SELECT statement.
  • You can use the comparison operators, such as >, <, or =. The comparison operator can also be a multiple-row operator, such as IN, NOT IN
  • A subquery can be treated as an inner query, which is a SQL query placed as a part of another query called as outer query.
  • The inner query executes first before its parent query so that the results of an inner query can be passed to the outer query.

Table of contents

Subquery Syntax

SQLite Subquery Example

Subqueries : Guidelines and Types of Subqueries

SQLite Subquery as Scalar Operand

SQLite Subqueries : Using Comparisons

SQLite Subqueries with IN, NOT IN

SQLite Subqueries with EXISTS

SQLite Correlated Subqueries

SQLite Subqueries in the FROM Clause

Subquery Syntax :

mysql subquery syntax

  • The subquery (inner query) executes once before the main query (outer query) executes.
  • The main query (outer query) use the subquery result.

SQLite Subquery Example :

Using a subquery, list the name of the employees, paid more than 'Alexander' (last_name) from employees :

Sample Table : employees

EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
100 Steven King SKING 515.123.4567 17-Jun-87 AD_PRES 24000     90
101 Neena Kochhar NKOCHHAR 515.123.4568 21-Sep-89 AD_VP 17000   100 90
102 Lex De Haan LDEHAAN 515.123.4569 13-Jan-93 AD_VP 17000   100 90
103 Alexander Hunold AHUNOLD 590.423.4567 3-Jan-90 IT_PROG 9000   102 60
104 Bruce Ernst BERNST 590.423.4568 21-May-91 IT_PROG 6000   103 60
105 David Austin DAUSTIN 590.423.4569 25-Jun-97 IT_PROG 4800   103 60
106 Valli Pataballa VPATABAL 590.423.4560 5-Feb-98 IT_PROG 4800   103 60
107 Diana Lorentz DLORENTZ 590.423.5567 7-Feb-99 IT_PROG 4200   103 60
108 Nancy Greenberg NGREENBE 515.124.4569 17-Aug-94 FI_MGR 12000   101 100
109 Daniel Faviet DFAVIET 515.124.4169 16-Aug-94 FI_ACCOUNT 9000   108 100
110 John Chen JCHEN 515.124.4269 28-Sep-97 FI_ACCOUNT 8200   108 100
111 Ismael Sciarra ISCIARRA 515.124.4369 30-Sep-97 FI_ACCOUNT 7700   108 100
112 Jose Manuel Urman JMURMAN 515.124.4469 7-Mar-98 FI_ACCOUNT 7800   108 100
113 Luis Popp LPOPP 515.124.4567 7-Dec-99 FI_ACCOUNT 6900   108 100
114 Den Raphaely DRAPHEAL 515.127.4561 7-Dec-94 PU_MAN 11000   100 30
115 Alexander Khoo AKHOO 515.127.4562 18-May-95 PU_CLERK 3100   114 30
116 Shelli Baida SBAIDA 515.127.4563 24-Dec-97 PU_CLERK 2900   114 30
117 Sigal Tobias STOBIAS 515.127.4564 24-Jul-97 PU_CLERK 2800   114 30
118 Guy Himuro GHIMURO 515.127.4565 15-Nov-98 PU_CLERK 2600   114 30
119 Karen Colmenares KCOLMENA 515.127.4566 10-Aug-99 PU_CLERK 2500   114 30
120 Matthew Weiss MWEISS 650.123.1234 18-Jul-96 ST_MAN 8000   100 50
121 Adam Fripp AFRIPP 650.123.2234 10-Apr-97 ST_MAN 8200   100 50
122 Payam Kaufling PKAUFLIN 650.123.3234 1-May-95 ST_MAN 7900   100 50
123 Shanta Vollman SVOLLMAN 650.123.4234 10-Oct-97 ST_MAN 6500   100 50
124 Kevin Mourgos KMOURGOS 650.123.5234 16-Nov-99 ST_MAN 5800   100 50
125 Julia Nayer JNAYER 650.124.1214 16-Jul-97 ST_CLERK 3200   120 50
126 Irene Mikkilineni IMIKKILI 650.124.1224 28-Sep-98 ST_CLERK 2700   120 50
127 James Landry JLANDRY 650.124.1334 14-Jan-99 ST_CLERK 2400   120 50
128 Steven Markle SMARKLE 650.124.1434 8-Mar-00 ST_CLERK 2200   120 50
129 Laura Bissot LBISSOT 650.124.5234 20-Aug-97 ST_CLERK 3300   121 50
130 Mozhe Atkinson MATKINSO 650.124.6234 30-Oct-97 ST_CLERK 2800   121 50
131 James Marlow JAMRLOW 650.124.7234 16-Feb-97 ST_CLERK 2500   121 50
132 TJ Olson TJOLSON 650.124.8234 10-Apr-99 ST_CLERK 2100   121 50
133 Jason Mallin JMALLIN 650.127.1934 14-Jun-96 ST_CLERK 3300   122 50
134 Michael Rogers MROGERS 650.127.1834 26-Aug-98 ST_CLERK 2900   122 50
135 Ki Gee KGEE 650.127.1734 12-Dec-99 ST_CLERK 2400   122 50
136 Hazel Philtanker HPHILTAN 650.127.1634 6-Feb-00 ST_CLERK 2200   122 50
137 Renske Ladwig RLADWIG 650.121.1234 14-Jul-95 ST_CLERK 3600   123 50
138 Stephen Stiles SSTILES 650.121.2034 26-Oct-97 ST_CLERK 3200   123 50
139 John Seo JSEO 650.121.2019 12-Feb-98 ST_CLERK 2700   123 50
140 Joshua Patel JPATEL 650.121.1834 6-Apr-98 ST_CLERK 2500   123 50
141 Trenna Rajs TRAJS 650.121.8009 17-Oct-95 ST_CLERK 3500   124 50
142 Curtis Davies CDAVIES 650.121.2994 29-Jan-97 ST_CLERK 3100   124 50
143 Randall Matos RMATOS 650.121.2874 15-Mar-98 ST_CLERK 2600   124 50
144 Peter Vargas PVARGAS 650.121.2004 9-Jul-98 ST_CLERK 2500   124 50
145 John Russell JRUSSEL 011.44.1344.429268 1-Oct-96 SA_MAN 14000 0.4 100 80
146 Karen Partners KPARTNER 011.44.1344.467268 5-Jan-97 SA_MAN 13500 0.3 100 80
147 Alberto Errazuriz AERRAZUR 011.44.1344.429278 10-Mar-97 SA_MAN 12000 0.3 100 80
148 Gerald Cambrault GCAMBRAU 011.44.1344.619268 15-Oct-99 SA_MAN 11000 0.3 100 80
149 Eleni Zlotkey EZLOTKEY 011.44.1344.429018 29-Jan-00 SA_MAN 10500 0.2 100 80
150 Peter Tucker PTUCKER 011.44.1344.129268 30-Jan-97 SA_REP 10000 0.3 145 80
151 David Bernstein DBERNSTE 011.44.1344.345268 24-Mar-97 SA_REP 9500 0.25 145 80
152 Peter Hall PHALL 011.44.1344.478968 20-Aug-97 SA_REP 9000 0.25 145 80
153 Christopher Olsen COLSEN 011.44.1344.498718 30-Mar-98 SA_REP 8000 0.2 145 80
154 Nanette Cambrault NCAMBRAU 011.44.1344.987668 9-Dec-98 SA_REP 7500 0.2 145 80
155 Oliver Tuvault OTUVAULT 011.44.1344.486508 23-Nov-99 SA_REP 7000 0.15 145 80
156 Janette King JKING 011.44.1345.429268 30-Jan-96 SA_REP 10000 0.35 146 80
157 Patrick Sully PSULLY 011.44.1345.929268 4-Mar-96 SA_REP 9500 0.35 146 80
158 Allan McEwen AMCEWEN 011.44.1345.829268 1-Aug-96 SA_REP 9000 0.35 146 80
159 Lindsey Smith LSMITH 011.44.1345.729268 10-Mar-97 SA_REP 8000 0.3 146 80
160 Louise Doran LDORAN 011.44.1345.629268 15-Dec-97 SA_REP 7500 0.3 146 80
161 Sarath Sewall SSEWALL 011.44.1345.529268 3-Nov-98 SA_REP 7000 0.25 146 80
162 Clara Vishney CVISHNEY 011.44.1346.129268 11-Nov-97 SA_REP 10500 0.25 147 80
163 Danielle Greene DGREENE 011.44.1346.229268 19-Mar-99 SA_REP 9500 0.15 147 80
164 Mattea Marvins MMARVINS 011.44.1346.329268 24-Jan-00 SA_REP 7200 0.1 147 80
165 David Lee DLEE 011.44.1346.529268 23-Feb-00 SA_REP 6800 0.1 147 80
166 Sundar Ande SANDE 011.44.1346.629268 24-Mar-00 SA_REP 6400 0.1 147 80
167 Amit Banda ABANDA 011.44.1346.729268 21-Apr-00 SA_REP 6200 0.1 147 80
168 Lisa Ozer LOZER 011.44.1343.929268 11-Mar-97 SA_REP 11500 0.25 148 80
169 Harrison Bloom HBLOOM 011.44.1343.829268 23-Mar-98 SA_REP 10000 0.2 148 80
170 Tayler Fox TFOX 011.44.1343.729268 24-Jan-98 SA_REP 9600 0.2 148 80
171 William Smith WSMITH 011.44.1343.629268 23-Feb-99 SA_REP 7400 0.15 148 80
172 Elizabeth Bates EBATES 011.44.1343.529268 24-Mar-99 SA_REP 7300 0.15 148 80
173 Sundita Kumar SKUMAR 011.44.1343.329268 21-Apr-00 SA_REP 6100 0.1 148 80
174 Ellen Abel EABEL 011.44.1644.429267 11-May-96 SA_REP 11000 0.3 149 80
175 Alyssa Hutton AHUTTON 011.44.1644.429266 19-Mar-97 SA_REP 8800 0.25 149 80
176 Jonathon Taylor JTAYLOR 011.44.1644.429265 24-Mar-98 SA_REP 8600 0.2 149 80
177 Jack Livingston JLIVINGS 011.44.1644.429264 23-Apr-98 SA_REP 8400 0.2 149 80
178 Kimberely Grant KGRANT 011.44.1644.429263 24-May-99 SA_REP 7000 0.15 149  
179 Charles Johnson CJOHNSON 011.44.1644.429262 4-Jan-00 SA_REP 6200 0.1 149 80
180 Winston Taylor WTAYLOR 650.507.9876 24-Jan-98 SH_CLERK 3200   120 50
181 Jean Fleaur JFLEAUR 650.507.9877 23-Feb-98 SH_CLERK 3100   120 50
182 Martha Sullivan MSULLIVA 650.507.9878 21-Jun-99 SH_CLERK 2500   120 50
183 Girard Geoni GGEONI 650.507.9879 3-Feb-00 SH_CLERK 2800   120 50
184 Nandita Sarchand NSARCHAN 650.509.1876 27-Jan-96 SH_CLERK 4200   121 50
185 Alexis Bull ABULL 650.509.2876 20-Feb-97 SH_CLERK 4100   121 50
186 Julia Dellinger JDELLING 650.509.3876 24-Jun-98 SH_CLERK 3400   121 50
187 Anthony Cabrio ACABRIO 650.509.4876 7-Feb-99 SH_CLERK 3000   121 50
188 Kelly Chung KCHUNG 650.505.1876 14-Jun-97 SH_CLERK 3800   122 50
189 Jennifer Dilly JDILLY 650.505.2876 13-Aug-97 SH_CLERK 3600   122 50
190 Timothy Gates TGATES 650.505.3876 11-Jul-98 SH_CLERK 2900   122 50
191 Randall Perkins RPERKINS 650.505.4876 19-Dec-99 SH_CLERK 2500   122 50
192 Sarah Bell SBELL 650.501.1876 4-Feb-96 SH_CLERK 4000   123 50
193 Britney Everett BEVERETT 650.501.2876 3-Mar-97 SH_CLERK 3900   123 50
194 Samuel McCain SMCCAIN 650.501.3876 1-Jul-98 SH_CLERK 3200   123 50
195 Vance Jones VJONES 650.501.4876 17-Mar-99 SH_CLERK 2800   123 50
196 Alana Walsh AWALSH 650.507.9811 24-Apr-98 SH_CLERK 3100   124 50
197 Kevin Feeney KFEENEY 650.507.9822 23-May-98 SH_CLERK 3000   124 50
198 Donald OConnell DOCONNEL 650.507.9833 21-Jun-99 SH_CLERK 2600   124 50
199 Douglas Grant DGRANT 650.507.9844 13-Jan-00 SH_CLERK 2600   124 50
200 Jennifer Whalen JWHALEN 515.123.4444 17-Sep-87 AD_ASST 4400   101 10
201 Michael Hartstein MHARTSTE 515.123.5555 17-Feb-96 MK_MAN 13000   100 20
202 Pat Fay PFAY 603.123.6666 17-Aug-97 MK_REP 6000   201 20
203 Susan Mavris SMAVRIS 515.123.7777 7-Jun-94 HR_REP 6500   101 40
204 Hermann Baer HBAER 515.123.8888 7-Jun-94 PR_REP 10000   101 70
205 Shelley Higgins SHIGGINS 515.123.8080 7-Jun-94 AC_MGR 12000   101 110
206 William Gietz WGIETZ 515.123.8181 7-Jun-94 AC_ACCOUNT 8300   205 110

sqlite subquery example

 

Code :

sqlite> SELECT first_name,last_name, salary 
FROM employees 
WHERE salary >(
SELECT salary FROM employees WHERE first_name='Alexander');

Output :

first_name  last_name   salary
----------  ----------  ----------
Steven      King        24000
Neena       Kochhar     17000
Lex         De Haan     17000
Nancy       Greenberg   12000
Den         Raphaely    11000
John        Russell     14000
Karen       Partners    13500
Alberto     Errazuriz   12000
Gerald      Cambrault   11000
Eleni       Zlotkey     10500
Peter       Tucker      10000
David       Bernstein   9500
Janette     King        10000
Patrick     Sully       9500
Clara       Vishney     10500
Danielle    Greene      9500
Lisa        Ozer        11500
Harrison    Bloom       10000
Tayler      Fox         9600
Ellen       Abel        11000
Michael     Hartstein   13000
Hermann     Baer        10000
Shelley     Higgins     12000

Subqueries: Guidelines

There are some guidelines to consider when using subqueries : 
- A subquery must be enclosed in parentheses.  
- Use single-row operators with single-row subqueries, and use multiple-row operators with multiple-row subqueries.
- If a subquery (inner query) returns a null value to the outer query, the outer query will not return any rows when using certain comparison operators in a WHERE clause.

Types of Subqueries

  • The Subquery as Scalar Operand
  • Comparisons using Subqueries
  • Subqueries with IN, NOT IN, EXISTS operator
  • Correlated Subqueries
  • Subqueries in the FROM Clause

SQLite Subquery as Scalar Operand

A SELECT statement enclosed in parentheses may appear as a scalar quantity. A scalar subquery is a subquery that returns exactly one column value from one row.

  • The SELECT query is executed and the single returned value is used in the surrounding value expression.
  • It is an error to use a query that returns more than one row or more than one column as a scalar subquery.
  • During a particular execution, if the subquery returns no rows, that is not an error; the scalar result is taken to be null.
  • The subquery can refer to variables from the surrounding query, which will act as constants during any one evaluation of the subquery.

Example :

sqlite> SELECT employee_id, last_name, 
(CASE WHEN department_id=( SELECT department_id from departments 
WHERE location_id=2500) THEN 'Canada' 
ELSE 'USA' END) location 
FROM employees;

Output :

employee_id  last_name   location
-----------  ----------  ----------
100          King        USA
101          Kochhar     USA
102          De Haan     USA
103          Hunold      USA
104          Ernst       USA
105          Austin      USA
106          Pataballa   USA
107          Lorentz     USA
-----------  ----------  ----------
-----------  ----------  ----------

SQLite Subqueries : Using Comparisons

A subquery can be used before or after any of the comparison operators. The subquery can return at most one value. The value can be the result of an arithmetic expression or a column function. SQL then compares the value that results from the subquery with the value on the other side of the comparison operator. You can use the following comparison operators :

Operator Description
= Test for equality.
== Test for equality.
> Greater than.
< Less than.
>= Greater than equal to.
<= Less than equal to.
<> Test for inequality.
!= Test for inequality.

For example, suppose you want to find the employee id, first_name, last_name, and salaries for employees whose average salary is higher than the average salary throughout the company.

mysql comparison operator

sqlite> SELECT employee_id,first_name,last_name,salary 
FROM employees 
WHERE salary > (SELECT AVG(SALARY) FROM employees);

Output :

employee_id  first_name  last_name   salary
-----------  ----------  ----------  ----------
100          Steven      King        24000
101          Neena       Kochhar     17000
102          Lex         De Haan     17000
103          Alexander   Hunold      9000
108          Nancy       Greenberg   12000
109          Daniel      Faviet      9000
110          John        Chen        8200
-----------  ----------  ----------  ----------
-----------  ----------  ----------  ----------

SQLite Subqueries with IN operator

Syntax :

expression IN (subquery)

The right-hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result.

  • The result of IN is true if any equal subquery row is found.
  • The result is "false" if no equal row is found (including the case where the subquery returns no rows).
  • If the left-hand expression yields null, or if there are no equal right-hand values and at least one right-hand row yields null, the result of the IN construct will be null, not false.

Example : SQLite Subquery, IN operator

The following query selects those employees who work in the location 1700. The subquery finds the department id in the 1700 location, and then the main query selects the employees who work in any of these departments.

Sample Table : employees

EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
100 Steven King SKING 515.123.4567 17-Jun-87 AD_PRES 24000     90
101 Neena Kochhar NKOCHHAR 515.123.4568 21-Sep-89 AD_VP 17000   100 90
102 Lex De Haan LDEHAAN 515.123.4569 13-Jan-93 AD_VP 17000   100 90
103 Alexander Hunold AHUNOLD 590.423.4567 3-Jan-90 IT_PROG 9000   102 60
104 Bruce Ernst BERNST 590.423.4568 21-May-91 IT_PROG 6000   103 60
105 David Austin DAUSTIN 590.423.4569 25-Jun-97 IT_PROG 4800   103 60
106 Valli Pataballa VPATABAL 590.423.4560 5-Feb-98 IT_PROG 4800   103 60
107 Diana Lorentz DLORENTZ 590.423.5567 7-Feb-99 IT_PROG 4200   103 60
108 Nancy Greenberg NGREENBE 515.124.4569 17-Aug-94 FI_MGR 12000   101 100
109 Daniel Faviet DFAVIET 515.124.4169 16-Aug-94 FI_ACCOUNT 9000   108 100
110 John Chen JCHEN 515.124.4269 28-Sep-97 FI_ACCOUNT 8200   108 100
111 Ismael Sciarra ISCIARRA 515.124.4369 30-Sep-97 FI_ACCOUNT 7700   108 100
112 Jose Manuel Urman JMURMAN 515.124.4469 7-Mar-98 FI_ACCOUNT 7800   108 100
113 Luis Popp LPOPP 515.124.4567 7-Dec-99 FI_ACCOUNT 6900   108 100
114 Den Raphaely DRAPHEAL 515.127.4561 7-Dec-94 PU_MAN 11000   100 30
115 Alexander Khoo AKHOO 515.127.4562 18-May-95 PU_CLERK 3100   114 30
116 Shelli Baida SBAIDA 515.127.4563 24-Dec-97 PU_CLERK 2900   114 30
117 Sigal Tobias STOBIAS 515.127.4564 24-Jul-97 PU_CLERK 2800   114 30
118 Guy Himuro GHIMURO 515.127.4565 15-Nov-98 PU_CLERK 2600   114 30
119 Karen Colmenares KCOLMENA 515.127.4566 10-Aug-99 PU_CLERK 2500   114 30
120 Matthew Weiss MWEISS 650.123.1234 18-Jul-96 ST_MAN 8000   100 50
121 Adam Fripp AFRIPP 650.123.2234 10-Apr-97 ST_MAN 8200   100 50
122 Payam Kaufling PKAUFLIN 650.123.3234 1-May-95 ST_MAN 7900   100 50
123 Shanta Vollman SVOLLMAN 650.123.4234 10-Oct-97 ST_MAN 6500   100 50
124 Kevin Mourgos KMOURGOS 650.123.5234 16-Nov-99 ST_MAN 5800   100 50
125 Julia Nayer JNAYER 650.124.1214 16-Jul-97 ST_CLERK 3200   120 50
126 Irene Mikkilineni IMIKKILI 650.124.1224 28-Sep-98 ST_CLERK 2700   120 50
127 James Landry JLANDRY 650.124.1334 14-Jan-99 ST_CLERK 2400   120 50
128 Steven Markle SMARKLE 650.124.1434 8-Mar-00 ST_CLERK 2200   120 50
129 Laura Bissot LBISSOT 650.124.5234 20-Aug-97 ST_CLERK 3300   121 50
130 Mozhe Atkinson MATKINSO 650.124.6234 30-Oct-97 ST_CLERK 2800   121 50
131 James Marlow JAMRLOW 650.124.7234 16-Feb-97 ST_CLERK 2500   121 50
132 TJ Olson TJOLSON 650.124.8234 10-Apr-99 ST_CLERK 2100   121 50
133 Jason Mallin JMALLIN 650.127.1934 14-Jun-96 ST_CLERK 3300   122 50
134 Michael Rogers MROGERS 650.127.1834 26-Aug-98 ST_CLERK 2900   122 50
135 Ki Gee KGEE 650.127.1734 12-Dec-99 ST_CLERK 2400   122 50
136 Hazel Philtanker HPHILTAN 650.127.1634 6-Feb-00 ST_CLERK 2200   122 50
137 Renske Ladwig RLADWIG 650.121.1234 14-Jul-95 ST_CLERK 3600   123 50
138 Stephen Stiles SSTILES 650.121.2034 26-Oct-97 ST_CLERK 3200   123 50
139 John Seo JSEO 650.121.2019 12-Feb-98 ST_CLERK 2700   123 50
140 Joshua Patel JPATEL 650.121.1834 6-Apr-98 ST_CLERK 2500   123 50
141 Trenna Rajs TRAJS 650.121.8009 17-Oct-95 ST_CLERK 3500   124 50
142 Curtis Davies CDAVIES 650.121.2994 29-Jan-97 ST_CLERK 3100   124 50
143 Randall Matos RMATOS 650.121.2874 15-Mar-98 ST_CLERK 2600   124 50
144 Peter Vargas PVARGAS 650.121.2004 9-Jul-98 ST_CLERK 2500   124 50
145 John Russell JRUSSEL 011.44.1344.429268 1-Oct-96 SA_MAN 14000 0.4 100 80
146 Karen Partners KPARTNER 011.44.1344.467268 5-Jan-97 SA_MAN 13500 0.3 100 80
147 Alberto Errazuriz AERRAZUR 011.44.1344.429278 10-Mar-97 SA_MAN 12000 0.3 100 80
148 Gerald Cambrault GCAMBRAU 011.44.1344.619268 15-Oct-99 SA_MAN 11000 0.3 100 80
149 Eleni Zlotkey EZLOTKEY 011.44.1344.429018 29-Jan-00 SA_MAN 10500 0.2 100 80
150 Peter Tucker PTUCKER 011.44.1344.129268 30-Jan-97 SA_REP 10000 0.3 145 80
151 David Bernstein DBERNSTE 011.44.1344.345268 24-Mar-97 SA_REP 9500 0.25 145 80
152 Peter Hall PHALL 011.44.1344.478968 20-Aug-97 SA_REP 9000 0.25 145 80
153 Christopher Olsen COLSEN 011.44.1344.498718 30-Mar-98 SA_REP 8000 0.2 145 80
154 Nanette Cambrault NCAMBRAU 011.44.1344.987668 9-Dec-98 SA_REP 7500 0.2 145 80
155 Oliver Tuvault OTUVAULT 011.44.1344.486508 23-Nov-99 SA_REP 7000 0.15 145 80
156 Janette King JKING 011.44.1345.429268 30-Jan-96 SA_REP 10000 0.35 146 80
157 Patrick Sully PSULLY 011.44.1345.929268 4-Mar-96 SA_REP 9500 0.35 146 80
158 Allan McEwen AMCEWEN 011.44.1345.829268 1-Aug-96 SA_REP 9000 0.35 146 80
159 Lindsey Smith LSMITH 011.44.1345.729268 10-Mar-97 SA_REP 8000 0.3 146 80
160 Louise Doran LDORAN 011.44.1345.629268 15-Dec-97 SA_REP 7500 0.3 146 80
161 Sarath Sewall SSEWALL 011.44.1345.529268 3-Nov-98 SA_REP 7000 0.25 146 80
162 Clara Vishney CVISHNEY 011.44.1346.129268 11-Nov-97 SA_REP 10500 0.25 147 80
163 Danielle Greene DGREENE 011.44.1346.229268 19-Mar-99 SA_REP 9500 0.15 147 80
164 Mattea Marvins MMARVINS 011.44.1346.329268 24-Jan-00 SA_REP 7200 0.1 147 80
165 David Lee DLEE 011.44.1346.529268 23-Feb-00 SA_REP 6800 0.1 147 80
166 Sundar Ande SANDE 011.44.1346.629268 24-Mar-00 SA_REP 6400 0.1 147 80
167 Amit Banda ABANDA 011.44.1346.729268 21-Apr-00 SA_REP 6200 0.1 147 80
168 Lisa Ozer LOZER 011.44.1343.929268 11-Mar-97 SA_REP 11500 0.25 148 80
169 Harrison Bloom HBLOOM 011.44.1343.829268 23-Mar-98 SA_REP 10000 0.2 148 80
170 Tayler Fox TFOX 011.44.1343.729268 24-Jan-98 SA_REP 9600 0.2 148 80
171 William Smith WSMITH 011.44.1343.629268 23-Feb-99 SA_REP 7400 0.15 148 80
172 Elizabeth Bates EBATES 011.44.1343.529268 24-Mar-99 SA_REP 7300 0.15 148 80
173 Sundita Kumar SKUMAR 011.44.1343.329268 21-Apr-00 SA_REP 6100 0.1 148 80
174 Ellen Abel EABEL 011.44.1644.429267 11-May-96 SA_REP 11000 0.3 149 80
175 Alyssa Hutton AHUTTON 011.44.1644.429266 19-Mar-97 SA_REP 8800 0.25 149 80
176 Jonathon Taylor JTAYLOR 011.44.1644.429265 24-Mar-98 SA_REP 8600 0.2 149 80
177 Jack Livingston JLIVINGS 011.44.1644.429264 23-Apr-98 SA_REP 8400 0.2 149 80
178 Kimberely Grant KGRANT 011.44.1644.429263 24-May-99 SA_REP 7000 0.15 149  
179 Charles Johnson CJOHNSON 011.44.1644.429262 4-Jan-00 SA_REP 6200 0.1 149 80
180 Winston Taylor WTAYLOR 650.507.9876 24-Jan-98 SH_CLERK 3200   120 50
181 Jean Fleaur JFLEAUR 650.507.9877 23-Feb-98 SH_CLERK 3100   120 50
182 Martha Sullivan MSULLIVA 650.507.9878 21-Jun-99 SH_CLERK 2500   120 50
183 Girard Geoni GGEONI 650.507.9879 3-Feb-00 SH_CLERK 2800   120 50
184 Nandita Sarchand NSARCHAN 650.509.1876 27-Jan-96 SH_CLERK 4200   121 50
185 Alexis Bull ABULL 650.509.2876 20-Feb-97 SH_CLERK 4100   121 50
186 Julia Dellinger JDELLING 650.509.3876 24-Jun-98 SH_CLERK 3400   121 50
187 Anthony Cabrio ACABRIO 650.509.4876 7-Feb-99 SH_CLERK 3000   121 50
188 Kelly Chung KCHUNG 650.505.1876 14-Jun-97 SH_CLERK 3800   122 50
189 Jennifer Dilly JDILLY 650.505.2876 13-Aug-97 SH_CLERK 3600   122 50
190 Timothy Gates TGATES 650.505.3876 11-Jul-98 SH_CLERK 2900   122 50
191 Randall Perkins RPERKINS 650.505.4876 19-Dec-99 SH_CLERK 2500   122 50
192 Sarah Bell SBELL 650.501.1876 4-Feb-96 SH_CLERK 4000   123 50
193 Britney Everett BEVERETT 650.501.2876 3-Mar-97 SH_CLERK 3900   123 50
194 Samuel McCain SMCCAIN 650.501.3876 1-Jul-98 SH_CLERK 3200   123 50
195 Vance Jones VJONES 650.501.4876 17-Mar-99 SH_CLERK 2800   123 50
196 Alana Walsh AWALSH 650.507.9811 24-Apr-98 SH_CLERK 3100   124 50
197 Kevin Feeney KFEENEY 650.507.9822 23-May-98 SH_CLERK 3000   124 50
198 Donald OConnell DOCONNEL 650.507.9833 21-Jun-99 SH_CLERK 2600   124 50
199 Douglas Grant DGRANT 650.507.9844 13-Jan-00 SH_CLERK 2600   124 50
200 Jennifer Whalen JWHALEN 515.123.4444 17-Sep-87 AD_ASST 4400   101 10
201 Michael Hartstein MHARTSTE 515.123.5555 17-Feb-96 MK_MAN 13000   100 20
202 Pat Fay PFAY 603.123.6666 17-Aug-97 MK_REP 6000   201 20
203 Susan Mavris SMAVRIS 515.123.7777 7-Jun-94 HR_REP 6500   101 40
204 Hermann Baer HBAER 515.123.8888 7-Jun-94 PR_REP 10000   101 70
205 Shelley Higgins SHIGGINS 515.123.8080 7-Jun-94 AC_MGR 12000   101 110
206 William Gietz WGIETZ 515.123.8181 7-Jun-94 AC_ACCOUNT 8300   205 110

Code :

sqlite> SELECT first_name, last_name,department_id
 ...> FROM employees
 ...> WHERE department_id IN
 ...> (SELECT DEPARTMENT_ID FROM departments
 ...> WHERE location_id=1700);
 

Output :

 first_name  last_name   department_id
 ----------  ----------  -------------
 Steven      King        90
 Neena       Kochhar     90
 Lex         De Haan     90
 Nancy       Greenberg   100
 Daniel      Faviet      100
 John        Chen        100
 Ismael      Sciarra     100
 Jose Manue  Urman       100
 Luis        Popp        100
 Den         Raphaely    30
 Alexander   Khoo        30
 Shelli      Baida       30
 Sigal       Tobias      30
 Guy         Himuro      30
 Karen       Colmenares  30
 Jennifer    Whalen      10
 Shelley     Higgins     110
 William     Gietz       110)

SQLite Subqueries with NOT IN operator

Syntax :

expression NOT IN (subquery)

The right-hand side is a parenthesized subquery, which must return exactly one column. The left-hand expression is evaluated and compared to each row of the subquery result.

  • The result of NOT IN is true if any equal subquery row is found.
  • The result is "false" if no equal row is found (including the case where the subquery returns no rows).
  • If the left-hand expression yields null, or if there are no equal right-hand values and at least one right-hand row yields null, the result of the NOT IN construct will be null, not false.

Example : SQLite Subquery, NOT IN operator

The following query selects those employees who do not work under the manager, whose id is between 100 and 200. The subquery finds the department id which is under the manager whose id is between 100 and 200, and then the main query selects the employees who do not work in any of these departments.

Sample Table : employees

EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
100 Steven King SKING 515.123.4567 17-Jun-87 AD_PRES 24000     90
101 Neena Kochhar NKOCHHAR 515.123.4568 21-Sep-89 AD_VP 17000   100 90
102 Lex De Haan LDEHAAN 515.123.4569 13-Jan-93 AD_VP 17000   100 90
103 Alexander Hunold AHUNOLD 590.423.4567 3-Jan-90 IT_PROG 9000   102 60
104 Bruce Ernst BERNST 590.423.4568 21-May-91 IT_PROG 6000   103 60
105 David Austin DAUSTIN 590.423.4569 25-Jun-97 IT_PROG 4800   103 60
106 Valli Pataballa VPATABAL 590.423.4560 5-Feb-98 IT_PROG 4800   103 60
107 Diana Lorentz DLORENTZ 590.423.5567 7-Feb-99 IT_PROG 4200   103 60
108 Nancy Greenberg NGREENBE 515.124.4569 17-Aug-94 FI_MGR 12000   101 100
109 Daniel Faviet DFAVIET 515.124.4169 16-Aug-94 FI_ACCOUNT 9000   108 100
110 John Chen JCHEN 515.124.4269 28-Sep-97 FI_ACCOUNT 8200   108 100
111 Ismael Sciarra ISCIARRA 515.124.4369 30-Sep-97 FI_ACCOUNT 7700   108 100
112 Jose Manuel Urman JMURMAN 515.124.4469 7-Mar-98 FI_ACCOUNT 7800   108 100
113 Luis Popp LPOPP 515.124.4567 7-Dec-99 FI_ACCOUNT 6900   108 100
114 Den Raphaely DRAPHEAL 515.127.4561 7-Dec-94 PU_MAN 11000   100 30
115 Alexander Khoo AKHOO 515.127.4562 18-May-95 PU_CLERK 3100   114 30
116 Shelli Baida SBAIDA 515.127.4563 24-Dec-97 PU_CLERK 2900   114 30
117 Sigal Tobias STOBIAS 515.127.4564 24-Jul-97 PU_CLERK 2800   114 30
118 Guy Himuro GHIMURO 515.127.4565 15-Nov-98 PU_CLERK 2600   114 30
119 Karen Colmenares KCOLMENA 515.127.4566 10-Aug-99 PU_CLERK 2500   114 30
120 Matthew Weiss MWEISS 650.123.1234 18-Jul-96 ST_MAN 8000   100 50
121 Adam Fripp AFRIPP 650.123.2234 10-Apr-97 ST_MAN 8200   100 50
122 Payam Kaufling PKAUFLIN 650.123.3234 1-May-95 ST_MAN 7900   100 50
123 Shanta Vollman SVOLLMAN 650.123.4234 10-Oct-97 ST_MAN 6500   100 50
124 Kevin Mourgos KMOURGOS 650.123.5234 16-Nov-99 ST_MAN 5800   100 50
125 Julia Nayer JNAYER 650.124.1214 16-Jul-97 ST_CLERK 3200   120 50
126 Irene Mikkilineni IMIKKILI 650.124.1224 28-Sep-98 ST_CLERK 2700   120 50
127 James Landry JLANDRY 650.124.1334 14-Jan-99 ST_CLERK 2400   120 50
128 Steven Markle SMARKLE 650.124.1434 8-Mar-00 ST_CLERK 2200   120 50
129 Laura Bissot LBISSOT 650.124.5234 20-Aug-97 ST_CLERK 3300   121 50
130 Mozhe Atkinson MATKINSO 650.124.6234 30-Oct-97 ST_CLERK 2800   121 50
131 James Marlow JAMRLOW 650.124.7234 16-Feb-97 ST_CLERK 2500   121 50
132 TJ Olson TJOLSON 650.124.8234 10-Apr-99 ST_CLERK 2100   121 50
133 Jason Mallin JMALLIN 650.127.1934 14-Jun-96 ST_CLERK 3300   122 50
134 Michael Rogers MROGERS 650.127.1834 26-Aug-98 ST_CLERK 2900   122 50
135 Ki Gee KGEE 650.127.1734 12-Dec-99 ST_CLERK 2400   122 50
136 Hazel Philtanker HPHILTAN 650.127.1634 6-Feb-00 ST_CLERK 2200   122 50
137 Renske Ladwig RLADWIG 650.121.1234 14-Jul-95 ST_CLERK 3600   123 50
138 Stephen Stiles SSTILES 650.121.2034 26-Oct-97 ST_CLERK 3200   123 50
139 John Seo JSEO 650.121.2019 12-Feb-98 ST_CLERK 2700   123 50
140 Joshua Patel JPATEL 650.121.1834 6-Apr-98 ST_CLERK 2500   123 50
141 Trenna Rajs TRAJS 650.121.8009 17-Oct-95 ST_CLERK 3500   124 50
142 Curtis Davies CDAVIES 650.121.2994 29-Jan-97 ST_CLERK 3100   124 50
143 Randall Matos RMATOS 650.121.2874 15-Mar-98 ST_CLERK 2600   124 50
144 Peter Vargas PVARGAS 650.121.2004 9-Jul-98 ST_CLERK 2500   124 50
145 John Russell JRUSSEL 011.44.1344.429268 1-Oct-96 SA_MAN 14000 0.4 100 80
146 Karen Partners KPARTNER 011.44.1344.467268 5-Jan-97 SA_MAN 13500 0.3 100 80
147 Alberto Errazuriz AERRAZUR 011.44.1344.429278 10-Mar-97 SA_MAN 12000 0.3 100 80
148 Gerald Cambrault GCAMBRAU 011.44.1344.619268 15-Oct-99 SA_MAN 11000 0.3 100 80
149 Eleni Zlotkey EZLOTKEY 011.44.1344.429018 29-Jan-00 SA_MAN 10500 0.2 100 80
150 Peter Tucker PTUCKER 011.44.1344.129268 30-Jan-97 SA_REP 10000 0.3 145 80
151 David Bernstein DBERNSTE 011.44.1344.345268 24-Mar-97 SA_REP 9500 0.25 145 80
152 Peter Hall PHALL 011.44.1344.478968 20-Aug-97 SA_REP 9000 0.25 145 80
153 Christopher Olsen COLSEN 011.44.1344.498718 30-Mar-98 SA_REP 8000 0.2 145 80
154 Nanette Cambrault NCAMBRAU 011.44.1344.987668 9-Dec-98 SA_REP 7500 0.2 145 80
155 Oliver Tuvault OTUVAULT 011.44.1344.486508 23-Nov-99 SA_REP 7000 0.15 145 80
156 Janette King JKING 011.44.1345.429268 30-Jan-96 SA_REP 10000 0.35 146 80
157 Patrick Sully PSULLY 011.44.1345.929268 4-Mar-96 SA_REP 9500 0.35 146 80
158 Allan McEwen AMCEWEN 011.44.1345.829268 1-Aug-96 SA_REP 9000 0.35 146 80
159 Lindsey Smith LSMITH 011.44.1345.729268 10-Mar-97 SA_REP 8000 0.3 146 80
160 Louise Doran LDORAN 011.44.1345.629268 15-Dec-97 SA_REP 7500 0.3 146 80
161 Sarath Sewall SSEWALL 011.44.1345.529268 3-Nov-98 SA_REP 7000 0.25 146 80
162 Clara Vishney CVISHNEY 011.44.1346.129268 11-Nov-97 SA_REP 10500 0.25 147 80
163 Danielle Greene DGREENE 011.44.1346.229268 19-Mar-99 SA_REP 9500 0.15 147 80
164 Mattea Marvins MMARVINS 011.44.1346.329268 24-Jan-00 SA_REP 7200 0.1 147 80
165 David Lee DLEE 011.44.1346.529268 23-Feb-00 SA_REP 6800 0.1 147 80
166 Sundar Ande SANDE 011.44.1346.629268 24-Mar-00 SA_REP 6400 0.1 147 80
167 Amit Banda ABANDA 011.44.1346.729268 21-Apr-00 SA_REP 6200 0.1 147 80
168 Lisa Ozer LOZER 011.44.1343.929268 11-Mar-97 SA_REP 11500 0.25 148 80
169 Harrison Bloom HBLOOM 011.44.1343.829268 23-Mar-98 SA_REP 10000 0.2 148 80
170 Tayler Fox TFOX 011.44.1343.729268 24-Jan-98 SA_REP 9600 0.2 148 80
171 William Smith WSMITH 011.44.1343.629268 23-Feb-99 SA_REP 7400 0.15 148 80
172 Elizabeth Bates EBATES 011.44.1343.529268 24-Mar-99 SA_REP 7300 0.15 148 80
173 Sundita Kumar SKUMAR 011.44.1343.329268 21-Apr-00 SA_REP 6100 0.1 148 80
174 Ellen Abel EABEL 011.44.1644.429267 11-May-96 SA_REP 11000 0.3 149 80
175 Alyssa Hutton AHUTTON 011.44.1644.429266 19-Mar-97 SA_REP 8800 0.25 149 80
176 Jonathon Taylor JTAYLOR 011.44.1644.429265 24-Mar-98 SA_REP 8600 0.2 149 80
177 Jack Livingston JLIVINGS 011.44.1644.429264 23-Apr-98 SA_REP 8400 0.2 149 80
178 Kimberely Grant KGRANT 011.44.1644.429263 24-May-99 SA_REP 7000 0.15 149  
179 Charles Johnson CJOHNSON 011.44.1644.429262 4-Jan-00 SA_REP 6200 0.1 149 80
180 Winston Taylor WTAYLOR 650.507.9876 24-Jan-98 SH_CLERK 3200   120 50
181 Jean Fleaur JFLEAUR 650.507.9877 23-Feb-98 SH_CLERK 3100   120 50
182 Martha Sullivan MSULLIVA 650.507.9878 21-Jun-99 SH_CLERK 2500   120 50
183 Girard Geoni GGEONI 650.507.9879 3-Feb-00 SH_CLERK 2800   120 50
184 Nandita Sarchand NSARCHAN 650.509.1876 27-Jan-96 SH_CLERK 4200   121 50
185 Alexis Bull ABULL 650.509.2876 20-Feb-97 SH_CLERK 4100   121 50
186 Julia Dellinger JDELLING 650.509.3876 24-Jun-98 SH_CLERK 3400   121 50
187 Anthony Cabrio ACABRIO 650.509.4876 7-Feb-99 SH_CLERK 3000   121 50
188 Kelly Chung KCHUNG 650.505.1876 14-Jun-97 SH_CLERK 3800   122 50
189 Jennifer Dilly JDILLY 650.505.2876 13-Aug-97 SH_CLERK 3600   122 50
190 Timothy Gates TGATES 650.505.3876 11-Jul-98 SH_CLERK 2900   122 50
191 Randall Perkins RPERKINS 650.505.4876 19-Dec-99 SH_CLERK 2500   122 50
192 Sarah Bell SBELL 650.501.1876 4-Feb-96 SH_CLERK 4000   123 50
193 Britney Everett BEVERETT 650.501.2876 3-Mar-97 SH_CLERK 3900   123 50
194 Samuel McCain SMCCAIN 650.501.3876 1-Jul-98 SH_CLERK 3200   123 50
195 Vance Jones VJONES 650.501.4876 17-Mar-99 SH_CLERK 2800   123 50
196 Alana Walsh AWALSH 650.507.9811 24-Apr-98 SH_CLERK 3100   124 50
197 Kevin Feeney KFEENEY 650.507.9822 23-May-98 SH_CLERK 3000   124 50
198 Donald OConnell DOCONNEL 650.507.9833 21-Jun-99 SH_CLERK 2600   124 50
199 Douglas Grant DGRANT 650.507.9844 13-Jan-00 SH_CLERK 2600   124 50
200 Jennifer Whalen JWHALEN 515.123.4444 17-Sep-87 AD_ASST 4400   101 10
201 Michael Hartstein MHARTSTE 515.123.5555 17-Feb-96 MK_MAN 13000   100 20
202 Pat Fay PFAY 603.123.6666 17-Aug-97 MK_REP 6000   201 20
203 Susan Mavris SMAVRIS 515.123.7777 7-Jun-94 HR_REP 6500   101 40
204 Hermann Baer HBAER 515.123.8888 7-Jun-94 PR_REP 10000   101 70
205 Shelley Higgins SHIGGINS 515.123.8080 7-Jun-94 AC_MGR 12000   101 110
206 William Gietz WGIETZ 515.123.8181 7-Jun-94 AC_ACCOUNT 8300   205 110

Sample Table : departments

DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID
10 Administration 200 1700
20 Marketing 201 1800
30 Purchasing 114 1700
40 Human Resources 203 2400
50 Shipping 121 1500
60 IT 103 1400
70 Public Relations 204 2700
80 Sales 145 2500
90 Executive 100 1700
100 Finance 108 1700
110 Accounting 205 1700
120 Treasury - 1700
130 Corporate Tax - 1700
140 Control And Credit - 1700
150 Shareholder Services - 1700
160 Benefits - 1700
170 Manufacturing - 1700
180 Construction - 1700
190 Contracting - 1700
200 Operations - 1700
210 IT Support - 1700
220 NOC - 1700
230 IT Helpdesk - 1700
240 Government Sales - 1700
250 Retail Sales - 1700
260 Recruiting - 1700
270 Payroll - 1700

Code :

sqlite> SELECT first_name, last_name,department_id
  ...> FROM employees
  ...> WHERE department_id NOT IN
  ...> (SELECT DEPARTMENT_ID FROM departments
  ...> WHERE manager_id
  ...> BETWEEN 100 AND 200);  

Output :

 
  first_name  last_name   department_id
  ----------  ----------  -------------
  Kimberely   Grant
  Michael     Hartstein   20
  Pat         Fay         20
  Susan       Mavris      40
  Hermann     Baer        70
  Shelley     Higgins     110
  William     Gietz       110
  

SQLite Subqueries with EXISTS operator

Syntax :

EXISTS (subquery)

The argument of EXISTS is an arbitrary SELECT statement or subquery. The subquery is evaluated to determine whether it returns any rows. If it returns at least one row, the result of EXISTS is true; if the subquery returns no rows, the result of EXISTS is false.

Example : SQLite Subqueries with EXISTS

From the following tables (employees) find employees (employee_id, first_name, last_name, job_id, department_id) who have at least one person reporting to them.
Sample table : employees table

EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
100 Steven King SKING 515.123.4567 17-Jun-87 AD_PRES 24000     90
101 Neena Kochhar NKOCHHAR 515.123.4568 21-Sep-89 AD_VP 17000   100 90
102 Lex De Haan LDEHAAN 515.123.4569 13-Jan-93 AD_VP 17000   100 90
103 Alexander Hunold AHUNOLD 590.423.4567 3-Jan-90 IT_PROG 9000   102 60
104 Bruce Ernst BERNST 590.423.4568 21-May-91 IT_PROG 6000   103 60
105 David Austin DAUSTIN 590.423.4569 25-Jun-97 IT_PROG 4800   103 60
106 Valli Pataballa VPATABAL 590.423.4560 5-Feb-98 IT_PROG 4800   103 60
107 Diana Lorentz DLORENTZ 590.423.5567 7-Feb-99 IT_PROG 4200   103 60
108 Nancy Greenberg NGREENBE 515.124.4569 17-Aug-94 FI_MGR 12000   101 100
109 Daniel Faviet DFAVIET 515.124.4169 16-Aug-94 FI_ACCOUNT 9000   108 100
110 John Chen JCHEN 515.124.4269 28-Sep-97 FI_ACCOUNT 8200   108 100
111 Ismael Sciarra ISCIARRA 515.124.4369 30-Sep-97 FI_ACCOUNT 7700   108 100
112 Jose Manuel Urman JMURMAN 515.124.4469 7-Mar-98 FI_ACCOUNT 7800   108 100
113 Luis Popp LPOPP 515.124.4567 7-Dec-99 FI_ACCOUNT 6900   108 100
114 Den Raphaely DRAPHEAL 515.127.4561 7-Dec-94 PU_MAN 11000   100 30
115 Alexander Khoo AKHOO 515.127.4562 18-May-95 PU_CLERK 3100   114 30
116 Shelli Baida SBAIDA 515.127.4563 24-Dec-97 PU_CLERK 2900   114 30
117 Sigal Tobias STOBIAS 515.127.4564 24-Jul-97 PU_CLERK 2800   114 30
118 Guy Himuro GHIMURO 515.127.4565 15-Nov-98 PU_CLERK 2600   114 30
119 Karen Colmenares KCOLMENA 515.127.4566 10-Aug-99 PU_CLERK 2500   114 30
120 Matthew Weiss MWEISS 650.123.1234 18-Jul-96 ST_MAN 8000   100 50
121 Adam Fripp AFRIPP 650.123.2234 10-Apr-97 ST_MAN 8200   100 50
122 Payam Kaufling PKAUFLIN 650.123.3234 1-May-95 ST_MAN 7900   100 50
123 Shanta Vollman SVOLLMAN 650.123.4234 10-Oct-97 ST_MAN 6500   100 50
124 Kevin Mourgos KMOURGOS 650.123.5234 16-Nov-99 ST_MAN 5800   100 50
125 Julia Nayer JNAYER 650.124.1214 16-Jul-97 ST_CLERK 3200   120 50
126 Irene Mikkilineni IMIKKILI 650.124.1224 28-Sep-98 ST_CLERK 2700   120 50
127 James Landry JLANDRY 650.124.1334 14-Jan-99 ST_CLERK 2400   120 50
128 Steven Markle SMARKLE 650.124.1434 8-Mar-00 ST_CLERK 2200   120 50
129 Laura Bissot LBISSOT 650.124.5234 20-Aug-97 ST_CLERK 3300   121 50
130 Mozhe Atkinson MATKINSO 650.124.6234 30-Oct-97 ST_CLERK 2800   121 50
131 James Marlow JAMRLOW 650.124.7234 16-Feb-97 ST_CLERK 2500   121 50
132 TJ Olson TJOLSON 650.124.8234 10-Apr-99 ST_CLERK 2100   121 50
133 Jason Mallin JMALLIN 650.127.1934 14-Jun-96 ST_CLERK 3300   122 50
134 Michael Rogers MROGERS 650.127.1834 26-Aug-98 ST_CLERK 2900   122 50
135 Ki Gee KGEE 650.127.1734 12-Dec-99 ST_CLERK 2400   122 50
136 Hazel Philtanker HPHILTAN 650.127.1634 6-Feb-00 ST_CLERK 2200   122 50
137 Renske Ladwig RLADWIG 650.121.1234 14-Jul-95 ST_CLERK 3600   123 50
138 Stephen Stiles SSTILES 650.121.2034 26-Oct-97 ST_CLERK 3200   123 50
139 John Seo JSEO 650.121.2019 12-Feb-98 ST_CLERK 2700   123 50
140 Joshua Patel JPATEL 650.121.1834 6-Apr-98 ST_CLERK 2500   123 50
141 Trenna Rajs TRAJS 650.121.8009 17-Oct-95 ST_CLERK 3500   124 50
142 Curtis Davies CDAVIES 650.121.2994 29-Jan-97 ST_CLERK 3100   124 50
143 Randall Matos RMATOS 650.121.2874 15-Mar-98 ST_CLERK 2600   124 50
144 Peter Vargas PVARGAS 650.121.2004 9-Jul-98 ST_CLERK 2500   124 50
145 John Russell JRUSSEL 011.44.1344.429268 1-Oct-96 SA_MAN 14000 0.4 100 80
146 Karen Partners KPARTNER 011.44.1344.467268 5-Jan-97 SA_MAN 13500 0.3 100 80
147 Alberto Errazuriz AERRAZUR 011.44.1344.429278 10-Mar-97 SA_MAN 12000 0.3 100 80
148 Gerald Cambrault GCAMBRAU 011.44.1344.619268 15-Oct-99 SA_MAN 11000 0.3 100 80
149 Eleni Zlotkey EZLOTKEY 011.44.1344.429018 29-Jan-00 SA_MAN 10500 0.2 100 80
150 Peter Tucker PTUCKER 011.44.1344.129268 30-Jan-97 SA_REP 10000 0.3 145 80
151 David Bernstein DBERNSTE 011.44.1344.345268 24-Mar-97 SA_REP 9500 0.25 145 80
152 Peter Hall PHALL 011.44.1344.478968 20-Aug-97 SA_REP 9000 0.25 145 80
153 Christopher Olsen COLSEN 011.44.1344.498718 30-Mar-98 SA_REP 8000 0.2 145 80
154 Nanette Cambrault NCAMBRAU 011.44.1344.987668 9-Dec-98 SA_REP 7500 0.2 145 80
155 Oliver Tuvault OTUVAULT 011.44.1344.486508 23-Nov-99 SA_REP 7000 0.15 145 80
156 Janette King JKING 011.44.1345.429268 30-Jan-96 SA_REP 10000 0.35 146 80
157 Patrick Sully PSULLY 011.44.1345.929268 4-Mar-96 SA_REP 9500 0.35 146 80
158 Allan McEwen AMCEWEN 011.44.1345.829268 1-Aug-96 SA_REP 9000 0.35 146 80
159 Lindsey Smith LSMITH 011.44.1345.729268 10-Mar-97 SA_REP 8000 0.3 146 80
160 Louise Doran LDORAN 011.44.1345.629268 15-Dec-97 SA_REP 7500 0.3 146 80
161 Sarath Sewall SSEWALL 011.44.1345.529268 3-Nov-98 SA_REP 7000 0.25 146 80
162 Clara Vishney CVISHNEY 011.44.1346.129268 11-Nov-97 SA_REP 10500 0.25 147 80
163 Danielle Greene DGREENE 011.44.1346.229268 19-Mar-99 SA_REP 9500 0.15 147 80
164 Mattea Marvins MMARVINS 011.44.1346.329268 24-Jan-00 SA_REP 7200 0.1 147 80
165 David Lee DLEE 011.44.1346.529268 23-Feb-00 SA_REP 6800 0.1 147 80
166 Sundar Ande SANDE 011.44.1346.629268 24-Mar-00 SA_REP 6400 0.1 147 80
167 Amit Banda ABANDA 011.44.1346.729268 21-Apr-00 SA_REP 6200 0.1 147 80
168 Lisa Ozer LOZER 011.44.1343.929268 11-Mar-97 SA_REP 11500 0.25 148 80
169 Harrison Bloom HBLOOM 011.44.1343.829268 23-Mar-98 SA_REP 10000 0.2 148 80
170 Tayler Fox TFOX 011.44.1343.729268 24-Jan-98 SA_REP 9600 0.2 148 80
171 William Smith WSMITH 011.44.1343.629268 23-Feb-99 SA_REP 7400 0.15 148 80
172 Elizabeth Bates EBATES 011.44.1343.529268 24-Mar-99 SA_REP 7300 0.15 148 80
173 Sundita Kumar SKUMAR 011.44.1343.329268 21-Apr-00 SA_REP 6100 0.1 148 80
174 Ellen Abel EABEL 011.44.1644.429267 11-May-96 SA_REP 11000 0.3 149 80
175 Alyssa Hutton AHUTTON 011.44.1644.429266 19-Mar-97 SA_REP 8800 0.25 149 80
176 Jonathon Taylor JTAYLOR 011.44.1644.429265 24-Mar-98 SA_REP 8600 0.2 149 80
177 Jack Livingston JLIVINGS 011.44.1644.429264 23-Apr-98 SA_REP 8400 0.2 149 80
178 Kimberely Grant KGRANT 011.44.1644.429263 24-May-99 SA_REP 7000 0.15 149  
179 Charles Johnson CJOHNSON 011.44.1644.429262 4-Jan-00 SA_REP 6200 0.1 149 80
180 Winston Taylor WTAYLOR 650.507.9876 24-Jan-98 SH_CLERK 3200   120 50
181 Jean Fleaur JFLEAUR 650.507.9877 23-Feb-98 SH_CLERK 3100   120 50
182 Martha Sullivan MSULLIVA 650.507.9878 21-Jun-99 SH_CLERK 2500   120 50
183 Girard Geoni GGEONI 650.507.9879 3-Feb-00 SH_CLERK 2800   120 50
184 Nandita Sarchand NSARCHAN 650.509.1876 27-Jan-96 SH_CLERK 4200   121 50
185 Alexis Bull ABULL 650.509.2876 20-Feb-97 SH_CLERK 4100   121 50
186 Julia Dellinger JDELLING 650.509.3876 24-Jun-98 SH_CLERK 3400   121 50
187 Anthony Cabrio ACABRIO 650.509.4876 7-Feb-99 SH_CLERK 3000   121 50
188 Kelly Chung KCHUNG 650.505.1876 14-Jun-97 SH_CLERK 3800   122 50
189 Jennifer Dilly JDILLY 650.505.2876 13-Aug-97 SH_CLERK 3600   122 50
190 Timothy Gates TGATES 650.505.3876 11-Jul-98 SH_CLERK 2900   122 50
191 Randall Perkins RPERKINS 650.505.4876 19-Dec-99 SH_CLERK 2500   122 50
192 Sarah Bell SBELL 650.501.1876 4-Feb-96 SH_CLERK 4000   123 50
193 Britney Everett BEVERETT 650.501.2876 3-Mar-97 SH_CLERK 3900   123 50
194 Samuel McCain SMCCAIN 650.501.3876 1-Jul-98 SH_CLERK 3200   123 50
195 Vance Jones VJONES 650.501.4876 17-Mar-99 SH_CLERK 2800   123 50
196 Alana Walsh AWALSH 650.507.9811 24-Apr-98 SH_CLERK 3100   124 50
197 Kevin Feeney KFEENEY 650.507.9822 23-May-98 SH_CLERK 3000   124 50
198 Donald OConnell DOCONNEL 650.507.9833 21-Jun-99 SH_CLERK 2600   124 50
199 Douglas Grant DGRANT 650.507.9844 13-Jan-00 SH_CLERK 2600   124 50
200 Jennifer Whalen JWHALEN 515.123.4444 17-Sep-87 AD_ASST 4400   101 10
201 Michael Hartstein MHARTSTE 515.123.5555 17-Feb-96 MK_MAN 13000   100 20
202 Pat Fay PFAY 603.123.6666 17-Aug-97 MK_REP 6000   201 20
203 Susan Mavris SMAVRIS 515.123.7777 7-Jun-94 HR_REP 6500   101 40
204 Hermann Baer HBAER 515.123.8888 7-Jun-94 PR_REP 10000   101 70
205 Shelley Higgins SHIGGINS 515.123.8080 7-Jun-94 AC_MGR 12000   101 110
206 William Gietz WGIETZ 515.123.8181 7-Jun-94 AC_ACCOUNT 8300   205 110

Code :

sqlite> SELECT employee_id, first_name, last_name, job_id, department_id
  ...> FROM employees E
  ...> WHERE EXISTS
  ...> (SELECT * FROM employees
  ...> WHERE manager_id = E.employee_id);
  

Output :

  employee_id  first_name  last_name   job_id      department_id
  -----------  ----------  ----------  ----------  -------------
  100          Steven      King        AD_PRES     90
  101          Neena       Kochhar     AD_VP       90
  102          Lex         De Haan     AD_VP       90
  103          Alexander   Hunold      IT_PROG     60
  108          Nancy       Greenberg   FI_MGR      100
  114          Den         Raphaely    PU_MAN      30
  120          Matthew     Weiss       ST_MAN      50
  121          Adam        Fripp       ST_MAN      50
  122          Payam       Kaufling    ST_MAN      50
  123          Shanta      Vollman     ST_MAN      50
  124          Kevin       Mourgos     ST_MAN      50
  145          John        Russell     SA_MAN      80
  146          Karen       Partners    SA_MAN      80
  147          Alberto     Errazuriz   SA_MAN      80
  148          Gerald      Cambrault   SA_MAN      80
  149          Eleni       Zlotkey     SA_MAN      80
  201          Michael     Hartstein   MK_MAN      20
  205          Shelley     Higgins     AC_MGR      110

SQLite Correlated Subqueries

A correlated subquery is a subquery that contains a reference to a table (in the parent query) that also appears in the outer query. SQLite evaluates from inside to outside.

Correlated subquery syntax :

SQLite Correlated Subqueries - w3resource

Example - 1 : SQLite Correlated Subqueries

Following query find all employees who earn more than the average salary in their department.

employees table :

EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
100 Steven King SKING 515.123.4567 17-Jun-87 AD_PRES 24000     90
101 Neena Kochhar NKOCHHAR 515.123.4568 21-Sep-89 AD_VP 17000   100 90
102 Lex De Haan LDEHAAN 515.123.4569 13-Jan-93 AD_VP 17000   100 90
103 Alexander Hunold AHUNOLD 590.423.4567 3-Jan-90 IT_PROG 9000   102 60
104 Bruce Ernst BERNST 590.423.4568 21-May-91 IT_PROG 6000   103 60
105 David Austin DAUSTIN 590.423.4569 25-Jun-97 IT_PROG 4800   103 60
106 Valli Pataballa VPATABAL 590.423.4560 5-Feb-98 IT_PROG 4800   103 60
107 Diana Lorentz DLORENTZ 590.423.5567 7-Feb-99 IT_PROG 4200   103 60
108 Nancy Greenberg NGREENBE 515.124.4569 17-Aug-94 FI_MGR 12000   101 100
109 Daniel Faviet DFAVIET 515.124.4169 16-Aug-94 FI_ACCOUNT 9000   108 100
110 John Chen JCHEN 515.124.4269 28-Sep-97 FI_ACCOUNT 8200   108 100
111 Ismael Sciarra ISCIARRA 515.124.4369 30-Sep-97 FI_ACCOUNT 7700   108 100
112 Jose Manuel Urman JMURMAN 515.124.4469 7-Mar-98 FI_ACCOUNT 7800   108 100
113 Luis Popp LPOPP 515.124.4567 7-Dec-99 FI_ACCOUNT 6900   108 100
114 Den Raphaely DRAPHEAL 515.127.4561 7-Dec-94 PU_MAN 11000   100 30
115 Alexander Khoo AKHOO 515.127.4562 18-May-95 PU_CLERK 3100   114 30
116 Shelli Baida SBAIDA 515.127.4563 24-Dec-97 PU_CLERK 2900   114 30
117 Sigal Tobias STOBIAS 515.127.4564 24-Jul-97 PU_CLERK 2800   114 30
118 Guy Himuro GHIMURO 515.127.4565 15-Nov-98 PU_CLERK 2600   114 30
119 Karen Colmenares KCOLMENA 515.127.4566 10-Aug-99 PU_CLERK 2500   114 30
120 Matthew Weiss MWEISS 650.123.1234 18-Jul-96 ST_MAN 8000   100 50
121 Adam Fripp AFRIPP 650.123.2234 10-Apr-97 ST_MAN 8200   100 50
122 Payam Kaufling PKAUFLIN 650.123.3234 1-May-95 ST_MAN 7900   100 50
123 Shanta Vollman SVOLLMAN 650.123.4234 10-Oct-97 ST_MAN 6500   100 50
124 Kevin Mourgos KMOURGOS 650.123.5234 16-Nov-99 ST_MAN 5800   100 50
125 Julia Nayer JNAYER 650.124.1214 16-Jul-97 ST_CLERK 3200   120 50
126 Irene Mikkilineni IMIKKILI 650.124.1224 28-Sep-98 ST_CLERK 2700   120 50
127 James Landry JLANDRY 650.124.1334 14-Jan-99 ST_CLERK 2400   120 50
128 Steven Markle SMARKLE 650.124.1434 8-Mar-00 ST_CLERK 2200   120 50
129 Laura Bissot LBISSOT 650.124.5234 20-Aug-97 ST_CLERK 3300   121 50
130 Mozhe Atkinson MATKINSO 650.124.6234 30-Oct-97 ST_CLERK 2800   121 50
131 James Marlow JAMRLOW 650.124.7234 16-Feb-97 ST_CLERK 2500   121 50
132 TJ Olson TJOLSON 650.124.8234 10-Apr-99 ST_CLERK 2100   121 50
133 Jason Mallin JMALLIN 650.127.1934 14-Jun-96 ST_CLERK 3300   122 50
134 Michael Rogers MROGERS 650.127.1834 26-Aug-98 ST_CLERK 2900   122 50
135 Ki Gee KGEE 650.127.1734 12-Dec-99 ST_CLERK 2400   122 50
136 Hazel Philtanker HPHILTAN 650.127.1634 6-Feb-00 ST_CLERK 2200   122 50
137 Renske Ladwig RLADWIG 650.121.1234 14-Jul-95 ST_CLERK 3600   123 50
138 Stephen Stiles SSTILES 650.121.2034 26-Oct-97 ST_CLERK 3200   123 50
139 John Seo JSEO 650.121.2019 12-Feb-98 ST_CLERK 2700   123 50
140 Joshua Patel JPATEL 650.121.1834 6-Apr-98 ST_CLERK 2500   123 50
141 Trenna Rajs TRAJS 650.121.8009 17-Oct-95 ST_CLERK 3500   124 50
142 Curtis Davies CDAVIES 650.121.2994 29-Jan-97 ST_CLERK 3100   124 50
143 Randall Matos RMATOS 650.121.2874 15-Mar-98 ST_CLERK 2600   124 50
144 Peter Vargas PVARGAS 650.121.2004 9-Jul-98 ST_CLERK 2500   124 50
145 John Russell JRUSSEL 011.44.1344.429268 1-Oct-96 SA_MAN 14000 0.4 100 80
146 Karen Partners KPARTNER 011.44.1344.467268 5-Jan-97 SA_MAN 13500 0.3 100 80
147 Alberto Errazuriz AERRAZUR 011.44.1344.429278 10-Mar-97 SA_MAN 12000 0.3 100 80
148 Gerald Cambrault GCAMBRAU 011.44.1344.619268 15-Oct-99 SA_MAN 11000 0.3 100 80
149 Eleni Zlotkey EZLOTKEY 011.44.1344.429018 29-Jan-00 SA_MAN 10500 0.2 100 80
150 Peter Tucker PTUCKER 011.44.1344.129268 30-Jan-97 SA_REP 10000 0.3 145 80
151 David Bernstein DBERNSTE 011.44.1344.345268 24-Mar-97 SA_REP 9500 0.25 145 80
152 Peter Hall PHALL 011.44.1344.478968 20-Aug-97 SA_REP 9000 0.25 145 80
153 Christopher Olsen COLSEN 011.44.1344.498718 30-Mar-98 SA_REP 8000 0.2 145 80
154 Nanette Cambrault NCAMBRAU 011.44.1344.987668 9-Dec-98 SA_REP 7500 0.2 145 80
155 Oliver Tuvault OTUVAULT 011.44.1344.486508 23-Nov-99 SA_REP 7000 0.15 145 80
156 Janette King JKING 011.44.1345.429268 30-Jan-96 SA_REP 10000 0.35 146 80
157 Patrick Sully PSULLY 011.44.1345.929268 4-Mar-96 SA_REP 9500 0.35 146 80
158 Allan McEwen AMCEWEN 011.44.1345.829268 1-Aug-96 SA_REP 9000 0.35 146 80
159 Lindsey Smith LSMITH 011.44.1345.729268 10-Mar-97 SA_REP 8000 0.3 146 80
160 Louise Doran LDORAN 011.44.1345.629268 15-Dec-97 SA_REP 7500 0.3 146 80
161 Sarath Sewall SSEWALL 011.44.1345.529268 3-Nov-98 SA_REP 7000 0.25 146 80
162 Clara Vishney CVISHNEY 011.44.1346.129268 11-Nov-97 SA_REP 10500 0.25 147 80
163 Danielle Greene DGREENE 011.44.1346.229268 19-Mar-99 SA_REP 9500 0.15 147 80
164 Mattea Marvins MMARVINS 011.44.1346.329268 24-Jan-00 SA_REP 7200 0.1 147 80
165 David Lee DLEE 011.44.1346.529268 23-Feb-00 SA_REP 6800 0.1 147 80
166 Sundar Ande SANDE 011.44.1346.629268 24-Mar-00 SA_REP 6400 0.1 147 80
167 Amit Banda ABANDA 011.44.1346.729268 21-Apr-00 SA_REP 6200 0.1 147 80
168 Lisa Ozer LOZER 011.44.1343.929268 11-Mar-97 SA_REP 11500 0.25 148 80
169 Harrison Bloom HBLOOM 011.44.1343.829268 23-Mar-98 SA_REP 10000 0.2 148 80
170 Tayler Fox TFOX 011.44.1343.729268 24-Jan-98 SA_REP 9600 0.2 148 80
171 William Smith WSMITH 011.44.1343.629268 23-Feb-99 SA_REP 7400 0.15 148 80
172 Elizabeth Bates EBATES 011.44.1343.529268 24-Mar-99 SA_REP 7300 0.15 148 80
173 Sundita Kumar SKUMAR 011.44.1343.329268 21-Apr-00 SA_REP 6100 0.1 148 80
174 Ellen Abel EABEL 011.44.1644.429267 11-May-96 SA_REP 11000 0.3 149 80
175 Alyssa Hutton AHUTTON 011.44.1644.429266 19-Mar-97 SA_REP 8800 0.25 149 80
176 Jonathon Taylor JTAYLOR 011.44.1644.429265 24-Mar-98 SA_REP 8600 0.2 149 80
177 Jack Livingston JLIVINGS 011.44.1644.429264 23-Apr-98 SA_REP 8400 0.2 149 80
178 Kimberely Grant KGRANT 011.44.1644.429263 24-May-99 SA_REP 7000 0.15 149  
179 Charles Johnson CJOHNSON 011.44.1644.429262 4-Jan-00 SA_REP 6200 0.1 149 80
180 Winston Taylor WTAYLOR 650.507.9876 24-Jan-98 SH_CLERK 3200   120 50
181 Jean Fleaur JFLEAUR 650.507.9877 23-Feb-98 SH_CLERK 3100   120 50
182 Martha Sullivan MSULLIVA 650.507.9878 21-Jun-99 SH_CLERK 2500   120 50
183 Girard Geoni GGEONI 650.507.9879 3-Feb-00 SH_CLERK 2800   120 50
184 Nandita Sarchand NSARCHAN 650.509.1876 27-Jan-96 SH_CLERK 4200   121 50
185 Alexis Bull ABULL 650.509.2876 20-Feb-97 SH_CLERK 4100   121 50
186 Julia Dellinger JDELLING 650.509.3876 24-Jun-98 SH_CLERK 3400   121 50
187 Anthony Cabrio ACABRIO 650.509.4876 7-Feb-99 SH_CLERK 3000   121 50
188 Kelly Chung KCHUNG 650.505.1876 14-Jun-97 SH_CLERK 3800   122 50
189 Jennifer Dilly JDILLY 650.505.2876 13-Aug-97 SH_CLERK 3600   122 50
190 Timothy Gates TGATES 650.505.3876 11-Jul-98 SH_CLERK 2900   122 50
191 Randall Perkins RPERKINS 650.505.4876 19-Dec-99 SH_CLERK 2500   122 50
192 Sarah Bell SBELL 650.501.1876 4-Feb-96 SH_CLERK 4000   123 50
193 Britney Everett BEVERETT 650.501.2876 3-Mar-97 SH_CLERK 3900   123 50
194 Samuel McCain SMCCAIN 650.501.3876 1-Jul-98 SH_CLERK 3200   123 50
195 Vance Jones VJONES 650.501.4876 17-Mar-99 SH_CLERK 2800   123 50
196 Alana Walsh AWALSH 650.507.9811 24-Apr-98 SH_CLERK 3100   124 50
197 Kevin Feeney KFEENEY 650.507.9822 23-May-98 SH_CLERK 3000   124 50
198 Donald OConnell DOCONNEL 650.507.9833 21-Jun-99 SH_CLERK 2600   124 50
199 Douglas Grant DGRANT 650.507.9844 13-Jan-00 SH_CLERK 2600   124 50
200 Jennifer Whalen JWHALEN 515.123.4444 17-Sep-87 AD_ASST 4400   101 10
201 Michael Hartstein MHARTSTE 515.123.5555 17-Feb-96 MK_MAN 13000   100 20
202 Pat Fay PFAY 603.123.6666 17-Aug-97 MK_REP 6000   201 20
203 Susan Mavris SMAVRIS 515.123.7777 7-Jun-94 HR_REP 6500   101 40
204 Hermann Baer HBAER 515.123.8888 7-Jun-94 PR_REP 10000   101 70
205 Shelley Higgins SHIGGINS 515.123.8080 7-Jun-94 AC_MGR 12000   101 110
206 William Gietz WGIETZ 515.123.8181 7-Jun-94 AC_ACCOUNT 8300   205 110

Code :

sqlite> SELECT last_name, salary, department_id
FROM employees outerr 
WHERE salary >
(SELECT AVG(salary) 
FROM employees 
WHERE department_id = outerr.department_id);
  

Output :

 last_name   salary      department_id
 ----------  ----------  -------------
 King        24000       90
 Hunold      9000        60
 Ernst       6000        60
 Greenberg   12000       100
 Faviet      9000        100
 Raphaely    11000       30
 Weiss       8000        50
 Fripp       8200        50
 Kaufling    7900        50
 Vollman     6500        50
 Mourgos     5800        50
 Ladwig      3600        50
 Rajs        3500        50
 Russell     14000       80
 Partners    13500       80
 Errazuriz   12000       80
 Cambrault   11000       80
 Zlotkey     10500       80
 Tucker      10000       80
 Bernstein   9500        80
 Hall        9000        80
 King        10000       80
 Sully       9500        80
 McEwen      9000        80
 Vishney     10500       80
 Greene      9500        80
 Ozer        11500       80
 Bloom       10000       80
 Fox         9600        80
 Abel        11000       80
 Sarchand    4200        50
 Bull        4100        50
 Chung       3800        50
 Dilly       3600        50
 Bell        4000        50
 Everett     3900        50
 Hartstein   13000       20
 Higgins     12000       110

Example - 2 : SQLite Correlated Subqueries

From the employees and job_history tables find details of those employees who have changed jobs at least once.

Sample table : employees table :

EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
100 Steven King SKING 515.123.4567 17-Jun-87 AD_PRES 24000     90
101 Neena Kochhar NKOCHHAR 515.123.4568 21-Sep-89 AD_VP 17000   100 90
102 Lex De Haan LDEHAAN 515.123.4569 13-Jan-93 AD_VP 17000   100 90
103 Alexander Hunold AHUNOLD 590.423.4567 3-Jan-90 IT_PROG 9000   102 60
104 Bruce Ernst BERNST 590.423.4568 21-May-91 IT_PROG 6000   103 60
105 David Austin DAUSTIN 590.423.4569 25-Jun-97 IT_PROG 4800   103 60
106 Valli Pataballa VPATABAL 590.423.4560 5-Feb-98 IT_PROG 4800   103 60
107 Diana Lorentz DLORENTZ 590.423.5567 7-Feb-99 IT_PROG 4200   103 60
108 Nancy Greenberg NGREENBE 515.124.4569 17-Aug-94 FI_MGR 12000   101 100
109 Daniel Faviet DFAVIET 515.124.4169 16-Aug-94 FI_ACCOUNT 9000   108 100
110 John Chen JCHEN 515.124.4269 28-Sep-97 FI_ACCOUNT 8200   108 100
111 Ismael Sciarra ISCIARRA 515.124.4369 30-Sep-97 FI_ACCOUNT 7700   108 100
112 Jose Manuel Urman JMURMAN 515.124.4469 7-Mar-98 FI_ACCOUNT 7800   108 100
113 Luis Popp LPOPP 515.124.4567 7-Dec-99 FI_ACCOUNT 6900   108 100
114 Den Raphaely DRAPHEAL 515.127.4561 7-Dec-94 PU_MAN 11000   100 30
115 Alexander Khoo AKHOO 515.127.4562 18-May-95 PU_CLERK 3100   114 30
116 Shelli Baida SBAIDA 515.127.4563 24-Dec-97 PU_CLERK 2900   114 30
117 Sigal Tobias STOBIAS 515.127.4564 24-Jul-97 PU_CLERK 2800   114 30
118 Guy Himuro GHIMURO 515.127.4565 15-Nov-98 PU_CLERK 2600   114 30
119 Karen Colmenares KCOLMENA 515.127.4566 10-Aug-99 PU_CLERK 2500   114 30
120 Matthew Weiss MWEISS 650.123.1234 18-Jul-96 ST_MAN 8000   100 50
121 Adam Fripp AFRIPP 650.123.2234 10-Apr-97 ST_MAN 8200   100 50
122 Payam Kaufling PKAUFLIN 650.123.3234 1-May-95 ST_MAN 7900   100 50
123 Shanta Vollman SVOLLMAN 650.123.4234 10-Oct-97 ST_MAN 6500   100 50
124 Kevin Mourgos KMOURGOS 650.123.5234 16-Nov-99 ST_MAN 5800   100 50
125 Julia Nayer JNAYER 650.124.1214 16-Jul-97 ST_CLERK 3200   120 50
126 Irene Mikkilineni IMIKKILI 650.124.1224 28-Sep-98 ST_CLERK 2700   120 50
127 James Landry JLANDRY 650.124.1334 14-Jan-99 ST_CLERK 2400   120 50
128 Steven Markle SMARKLE 650.124.1434 8-Mar-00 ST_CLERK 2200   120 50
129 Laura Bissot LBISSOT 650.124.5234 20-Aug-97 ST_CLERK 3300   121 50
130 Mozhe Atkinson MATKINSO 650.124.6234 30-Oct-97 ST_CLERK 2800   121 50
131 James Marlow JAMRLOW 650.124.7234 16-Feb-97 ST_CLERK 2500   121 50
132 TJ Olson TJOLSON 650.124.8234 10-Apr-99 ST_CLERK 2100   121 50
133 Jason Mallin JMALLIN 650.127.1934 14-Jun-96 ST_CLERK 3300   122 50
134 Michael Rogers MROGERS 650.127.1834 26-Aug-98 ST_CLERK 2900   122 50
135 Ki Gee KGEE 650.127.1734 12-Dec-99 ST_CLERK 2400   122 50
136 Hazel Philtanker HPHILTAN 650.127.1634 6-Feb-00 ST_CLERK 2200   122 50
137 Renske Ladwig RLADWIG 650.121.1234 14-Jul-95 ST_CLERK 3600   123 50
138 Stephen Stiles SSTILES 650.121.2034 26-Oct-97 ST_CLERK 3200   123 50
139 John Seo JSEO 650.121.2019 12-Feb-98 ST_CLERK 2700   123 50
140 Joshua Patel JPATEL 650.121.1834 6-Apr-98 ST_CLERK 2500   123 50
141 Trenna Rajs TRAJS 650.121.8009 17-Oct-95 ST_CLERK 3500   124 50
142 Curtis Davies CDAVIES 650.121.2994 29-Jan-97 ST_CLERK 3100   124 50
143 Randall Matos RMATOS 650.121.2874 15-Mar-98 ST_CLERK 2600   124 50
144 Peter Vargas PVARGAS 650.121.2004 9-Jul-98 ST_CLERK 2500   124 50
145 John Russell JRUSSEL 011.44.1344.429268 1-Oct-96 SA_MAN 14000 0.4 100 80
146 Karen Partners KPARTNER 011.44.1344.467268 5-Jan-97 SA_MAN 13500 0.3 100 80
147 Alberto Errazuriz AERRAZUR 011.44.1344.429278 10-Mar-97 SA_MAN 12000 0.3 100 80
148 Gerald Cambrault GCAMBRAU 011.44.1344.619268 15-Oct-99 SA_MAN 11000 0.3 100 80
149 Eleni Zlotkey EZLOTKEY 011.44.1344.429018 29-Jan-00 SA_MAN 10500 0.2 100 80
150 Peter Tucker PTUCKER 011.44.1344.129268 30-Jan-97 SA_REP 10000 0.3 145 80
151 David Bernstein DBERNSTE 011.44.1344.345268 24-Mar-97 SA_REP 9500 0.25 145 80
152 Peter Hall PHALL 011.44.1344.478968 20-Aug-97 SA_REP 9000 0.25 145 80
153 Christopher Olsen COLSEN 011.44.1344.498718 30-Mar-98 SA_REP 8000 0.2 145 80
154 Nanette Cambrault NCAMBRAU 011.44.1344.987668 9-Dec-98 SA_REP 7500 0.2 145 80
155 Oliver Tuvault OTUVAULT 011.44.1344.486508 23-Nov-99 SA_REP 7000 0.15 145 80
156 Janette King JKING 011.44.1345.429268 30-Jan-96 SA_REP 10000 0.35 146 80
157 Patrick Sully PSULLY 011.44.1345.929268 4-Mar-96 SA_REP 9500 0.35 146 80
158 Allan McEwen AMCEWEN 011.44.1345.829268 1-Aug-96 SA_REP 9000 0.35 146 80
159 Lindsey Smith LSMITH 011.44.1345.729268 10-Mar-97 SA_REP 8000 0.3 146 80
160 Louise Doran LDORAN 011.44.1345.629268 15-Dec-97 SA_REP 7500 0.3 146 80
161 Sarath Sewall SSEWALL 011.44.1345.529268 3-Nov-98 SA_REP 7000 0.25 146 80
162 Clara Vishney CVISHNEY 011.44.1346.129268 11-Nov-97 SA_REP 10500 0.25 147 80
163 Danielle Greene DGREENE 011.44.1346.229268 19-Mar-99 SA_REP 9500 0.15 147 80
164 Mattea Marvins MMARVINS 011.44.1346.329268 24-Jan-00 SA_REP 7200 0.1 147 80
165 David Lee DLEE 011.44.1346.529268 23-Feb-00 SA_REP 6800 0.1 147 80
166 Sundar Ande SANDE 011.44.1346.629268 24-Mar-00 SA_REP 6400 0.1 147 80
167 Amit Banda ABANDA 011.44.1346.729268 21-Apr-00 SA_REP 6200 0.1 147 80
168 Lisa Ozer LOZER 011.44.1343.929268 11-Mar-97 SA_REP 11500 0.25 148 80
169 Harrison Bloom HBLOOM 011.44.1343.829268 23-Mar-98 SA_REP 10000 0.2 148 80
170 Tayler Fox TFOX 011.44.1343.729268 24-Jan-98 SA_REP 9600 0.2 148 80
171 William Smith WSMITH 011.44.1343.629268 23-Feb-99 SA_REP 7400 0.15 148 80
172 Elizabeth Bates EBATES 011.44.1343.529268 24-Mar-99 SA_REP 7300 0.15 148 80
173 Sundita Kumar SKUMAR 011.44.1343.329268 21-Apr-00 SA_REP 6100 0.1 148 80
174 Ellen Abel EABEL 011.44.1644.429267 11-May-96 SA_REP 11000 0.3 149 80
175 Alyssa Hutton AHUTTON 011.44.1644.429266 19-Mar-97 SA_REP 8800 0.25 149 80
176 Jonathon Taylor JTAYLOR 011.44.1644.429265 24-Mar-98 SA_REP 8600 0.2 149 80
177 Jack Livingston JLIVINGS 011.44.1644.429264 23-Apr-98 SA_REP 8400 0.2 149 80
178 Kimberely Grant KGRANT 011.44.1644.429263 24-May-99 SA_REP 7000 0.15 149  
179 Charles Johnson CJOHNSON 011.44.1644.429262 4-Jan-00 SA_REP 6200 0.1 149 80
180 Winston Taylor WTAYLOR 650.507.9876 24-Jan-98 SH_CLERK 3200   120 50
181 Jean Fleaur JFLEAUR 650.507.9877 23-Feb-98 SH_CLERK 3100   120 50
182 Martha Sullivan MSULLIVA 650.507.9878 21-Jun-99 SH_CLERK 2500   120 50
183 Girard Geoni GGEONI 650.507.9879 3-Feb-00 SH_CLERK 2800   120 50
184 Nandita Sarchand NSARCHAN 650.509.1876 27-Jan-96 SH_CLERK 4200   121 50
185 Alexis Bull ABULL 650.509.2876 20-Feb-97 SH_CLERK 4100   121 50
186 Julia Dellinger JDELLING 650.509.3876 24-Jun-98 SH_CLERK 3400   121 50
187 Anthony Cabrio ACABRIO 650.509.4876 7-Feb-99 SH_CLERK 3000   121 50
188 Kelly Chung KCHUNG 650.505.1876 14-Jun-97 SH_CLERK 3800   122 50
189 Jennifer Dilly JDILLY 650.505.2876 13-Aug-97 SH_CLERK 3600   122 50
190 Timothy Gates TGATES 650.505.3876 11-Jul-98 SH_CLERK 2900   122 50
191 Randall Perkins RPERKINS 650.505.4876 19-Dec-99 SH_CLERK 2500   122 50
192 Sarah Bell SBELL 650.501.1876 4-Feb-96 SH_CLERK 4000   123 50
193 Britney Everett BEVERETT 650.501.2876 3-Mar-97 SH_CLERK 3900   123 50
194 Samuel McCain SMCCAIN 650.501.3876 1-Jul-98 SH_CLERK 3200   123 50
195 Vance Jones VJONES 650.501.4876 17-Mar-99 SH_CLERK 2800   123 50
196 Alana Walsh AWALSH 650.507.9811 24-Apr-98 SH_CLERK 3100   124 50
197 Kevin Feeney KFEENEY 650.507.9822 23-May-98 SH_CLERK 3000   124 50
198 Donald OConnell DOCONNEL 650.507.9833 21-Jun-99 SH_CLERK 2600   124 50
199 Douglas Grant DGRANT 650.507.9844 13-Jan-00 SH_CLERK 2600   124 50
200 Jennifer Whalen JWHALEN 515.123.4444 17-Sep-87 AD_ASST 4400   101 10
201 Michael Hartstein MHARTSTE 515.123.5555 17-Feb-96 MK_MAN 13000   100 20
202 Pat Fay PFAY 603.123.6666 17-Aug-97 MK_REP 6000   201 20
203 Susan Mavris SMAVRIS 515.123.7777 7-Jun-94 HR_REP 6500   101 40
204 Hermann Baer HBAER 515.123.8888 7-Jun-94 PR_REP 10000   101 70
205 Shelley Higgins SHIGGINS 515.123.8080 7-Jun-94 AC_MGR 12000   101 110
206 William Gietz WGIETZ 515.123.8181 7-Jun-94 AC_ACCOUNT 8300   205 110

Sample table : job_history table :

employee_id  start_date  end_date    job_id      department_id
-----------  ----------  ----------  ----------  -------------
employee_id  start_date  end_date`   job_id      department_id
102          1993-01-13  1998-07-24  IT_PROG     60
101          1989-09-21  1993-10-27  AC_ACCOUNT  110
101          1993-10-28  1997-03-15  AC_MGR      110
201          1996-02-17  1999-12-19  MK_REP      20
114          1998-03-24  1999-12-31  ST_CLERK    50
122          1999-01-01  1999-12-31  ST_CLERK    50
200          1987-09-17  1993-06-17  AD_ASST     90
176          1998-03-24  1998-12-31  SA_REP      80
176          1999-01-01  1999-12-31  SA_MAN      80
200          1994-07-01  1998-12-31  AC_ACCOUNT  90

Code :

sqlite> SELECT first_name, last_name, employee_id, job_id
 ...> FROM employees E
 ...> WHERE 1 <=
 ...> (SELECT COUNT(*) FROM Job_history
 ...> WHERE employee_id = E.employee_id);

output :

  
 first_name  last_name   employee_id  job_id
 ----------  ----------  -----------  ----------
 Neena       Kochhar     101          AD_VP
 Lex         De Haan     102          AD_VP
 Den         Raphaely    114          PU_MAN
 Payam       Kaufling    122          ST_MAN
 Jonathon    Taylor      176          SA_REP
 Jennifer    Whalen      200          AD_ASST
 Michael     Hartstein   201          MK_MAN
 

SQLite Subqueries in the FROM Clause

Subqueries work in a SELECT statement's FROM clause. The syntax is :

SELECT ... FROM (subquery) [AS] name ...

Every table in a FROM clause must have a name, therefore the [AS] name clause is mandatory. Any columns in the subquery select list must have unique names.

Example : SQLite Subqueries in the FROM Clause

We have the following table tb1.

sqlite> CREATE TABLE tb1 (c1 INT, c2 CHAR(5), c3 FLOAT);

Let insert some values into tb1.

sqlite> INSERT INTO tb1 VALUES (1, '1', 1.0);
sqlite> INSERT INTO tb1 VALUES (2, '2', 2.0);
sqlite> INSERT INTO tb1 VALUES (3, '3', 3.0);
sqlite> select * from tb1;
c1          c2          c3
----------  ----------  ----------
1           1           1.0
2           2           2.0
3           3           3.0

Here is how to use a subquery in the FROM clause, using the example table (tb1) :

sqlite> SELECT sc1, sc2, sc3 FROM (SELECT c1 AS sc1, c2 AS sc2, c3*3 AS sc3 FROM tb1) AS sb WHERE sc1 > 1;
sc1         sc2         sc3
----------  ----------  ----------
2           2           6.0
3           3           9.0