JournalSQLSubqueries and CTEs15 min

Subqueries (All You Need to Know)

In PostgreSQL, a subquery is a query within a query that allows you to construct complex queries. You can create subqueries within your SQL statements. These subqueries can reside in the WHERE clause, the FROM clause, or the SELECT clause.

  • WHERE subquery
  • IN
  • EXISTS
  • FROM clause

Based on Lesson 7.6 Video: More On Subqueries:

Expert Tip

  • Note that you should not include an alias when you write a subquery in a conditional statement. This is because the subquery is treated as an individual value (or set of values in the IN case) rather than as a table.
  • Also, notice the query here compared a single value. If we returned an entire column IN would need to be used to perform a logical argument. If we are returning an entire table, then we must use an ALIAS for the table, and perform additional logic on the entire table.

Topics:

  • In WHERE clause
    • Comparison Operators
    • IN operator
    • EXISTS operator
    • Row Subqueries
  • In FROM clause
  • In SELECT clause

What is a subquery?

In PostgreSQL, a subquery is a query within a query that allows you to construct complex queries. You can create subqueries within your SQL statements. These subqueries can reside in the WHERE clause, the FROM clause, or the SELECT clause.

WHERE clause

Comparison Operators

Suppose we want to find the films whose rental rate is higher than the average rental rate. We can do it in two steps:

  • Find the average rental rate by using the SELECT statement and average function ( AVG).
  • Use the result of the first query in the second SELECT statement to find the films that we want.
SELECT AVG(rental_rate)
FROM film;

The average rental rate is 2.98

Now, we can get films whose rental rate is higher than the average rental rate:

SELECT film_id, title, rental_rate
FROM film
WHERE rental_rate > 2.98;

Result:

The code is not so elegant, which requires two steps. We want a way to pass the result of the first query to the second query in one query. The solution is to use a subquery.

To construct a subquery, we put the second query in brackets and use it in the WHERE clause as an expression:

SELECT film_id, title, rental_rate
FROM film
WHERE rental_rate > (SELECT AVG(rental_rate)
                     FROM film);

Sample Tables

 employee_id | first_name  |  last_name  |        email         |    phone_number    | hire_date  |   job_id   |  salary  | commission_pct | manager_id | department_id
-------------+-------------+-------------+----------------------+--------------------+------------+------------+----------+----------------+------------+---------------
         100 | Steven      | King        | SKING@example.com    | 515.123.4567       | 1987-06-17 | AD_PRES    | 24000.00 |           0.00 |          0 |            90
         101 | Neena       | Kochhar     | NKOCHHAR@example.com | 515.123.4568       | 1987-06-18 | AD_VP      | 17000.00 |           0.00 |        100 |            90
         102 | Lex         | De Haan     | LDEHAAN@example.com  | 515.123.4569       | 1987-06-19 | AD_VP      | 17000.00 |           0.00 |        100 |            90
         103 | Alexander   | Hunold      | AHUNOLD@example.com  | 590.423.4567       | 1987-06-20 | IT_PROG    |  9000.00 |           0.00 |        102 |            60
         104 | Bruce       | Ernst       | BERNST@example.com   | 590.423.4568       | 1987-06-21 | IT_PROG    |  6000.00 |           0.00 |        103 |            60
         105 | David       | Austin      | DAUSTIN@example.com  | 590.423.4569       | 1987-06-22 | IT_PROG    |  4800.00 |           0.00 |        103 |            60
         106 | Valli       | Pataballa   | VPATABAL@example.com | 590.423.4560       | 1987-06-23 | IT_PROG    |  4800.00 |           0.00 |        103 |            60
         107 | Diana       | Lorentz     | DLORENTZ@example.com | 590.423.5567       | 1987-06-24 | IT_PROG    |  4200.00 |           0.00 |        103 |            60
         114 | Den         | Raphaely    | DRAPHEAL@example.com | 515.127.4561       | 1987-07-01 | PU_MAN     | 11000.00 |           0.00 |        100 |            30
         115 | Alexander   | Khoo        | AKHOO@example.com    | 515.127.4562       | 1987-07-02 | PU_CLERK   |  3100.00 |           0.00 |        114 |            30
         116 | Shelli      | Baida       | SBAIDA@example.com   | 515.127.4563       | 1987-07-03 | PU_CLERK   |  2900.00 |           0.00 |        114 |            30
         117 | Sigal       | Tobias      | STOBIAS@example.com  | 515.127.4564       | 1987-07-04 | PU_CLERK   |  2800.00 |           0.00 |        114 |            30
         118 | Guy         | Himuro      | GHIMURO@example.com  | 515.127.4565       | 1987-07-05 | PU_CLERK   |  2600.00 |           0.00 |        114 |            30
         119 | Karen       | Colmenares  | KCOLMENA@example.com | 515.127.4566       | 1987-07-06 | PU_CLERK   |  2500.00 |           0.00 |        114 |            30
         120 | Matthew     | Weiss       | MWEISS@example.com   | 650.123.1234       | 1987-07-07 | ST_MAN     |  8000.00 |           0.00 |        100 |            50
         121 | Adam        | Fripp       | AFRIPP@example.com   | 650.123.2234       | 1987-07-08 | ST_MAN     |  8200.00 |           0.00 |        100 |            50
         122 | Payam       | Kaufling    | PKAUFLIN@example.com | 650.123.3234       | 1987-07-09 | ST_MAN     |  7900.00 |           0.00 |        100 |            50
         123 | Shanta      | Vollman     | SVOLLMAN@example.com | 650.123.4234       | 1987-07-10 | ST_MAN     |  6500.00 |           0.00 |        100 |            50
         124 | Kevin       | Mourgos     | KMOURGOS@example.com | 650.123.5234       | 1987-07-11 | ST_MAN     |  5800.00 |           0.00 |        100 |            50
         133 | Jason       | Mallin      | JMALLIN@example.com  | 650.127.1934       | 1987-07-20 | ST_CLERK   |  3300.00 |           0.00 |        122 |            50
         134 | Michael     | Rogers      | MROGERS@example.com  | 650.127.1834       | 1987-07-21 | ST_CLERK   |  2900.00 |           0.00 |        122 |            50
         135 | Ki          | Gee         | KGEE@example.com     | 650.127.1734       | 1987-07-22 | ST_CLERK   |  2400.00 |           0.00 |        122 |            50
         136 | Hazel       | Philtanker  | HPHILTAN@example.com | 650.127.1634       | 1987-07-23 | ST_CLERK   |  2200.00 |           0.00 |        122 |            50
         137 | Renske      | Ladwig      | RLADWIG@example.com  | 650.121.1234       | 1987-07-24 | ST_CLERK   |  3600.00 |           0.00 |        123 |            50
         138 | Stephen     | Stiles      | SSTILES@example.com  | 650.121.2034       | 1987-07-25 | ST_CLERK   |  3200.00 |           0.00 |        123 |            50
         139 | John        | Seo         | JSEO@example.com     | 650.121.2019       | 1987-07-26 | ST_CLERK   |  2700.00 |           0.00 |        123 |            50
         140 | Joshua      | Patel       | JPATEL@example.com   | 650.121.1834       | 1987-07-27 | ST_CLERK   |  2500.00 |           0.00 |        123 |            50
         141 | Trenna      | Rajs        | TRAJS@example.com    | 650.121.8009       | 1987-07-28 | ST_CLERK   |  3500.00 |           0.00 |        124 |            50
         142 | Curtis      | Davies      | CDAVIES@example.com  | 650.121.2994       | 1987-07-29 | ST_CLERK   |  3100.00 |           0.00 |        124 |            50
         143 | Randall     | Matos       | RMATOS@example.com   | 650.121.2874       | 1987-07-30 | ST_CLERK   |  2600.00 |           0.00 |        124 |            50
         144 | Peter       | Vargas      | PVARGAS@example.com  | 650.121.2004       | 1987-07-31 | ST_CLERK   |  2500.00 |           0.00 |        124 |            50
         145 | John        | Russell     | JRUSSEL@example.com  | 011.44.1344.429268 | 1987-08-01 | SA_MAN     | 14000.00 |           0.40 |        100 |            80
         146 | Karen       | Partners    | KPARTNER@example.com | 011.44.1344.467268 | 1987-08-02 | SA_MAN     | 13500.00 |           0.30 |        100 |            80
         147 | Alberto     | Errazuriz   | AERRAZUR@example.com | 011.44.1344.429278 | 1987-08-03 | SA_MAN     | 12000.00 |           0.30 |        100 |            80
         148 | Gerald      | Cambrault   | GCAMBRAU@example.com | 011.44.1344.619268 | 1987-08-04 | SA_MAN     | 11000.00 |           0.30 |        100 |            80
         149 | Eleni       | Zlotkey     | EZLOTKEY@example.com | 011.44.1344.429018 | 1987-08-05 | SA_MAN     | 10500.00 |           0.20 |        100 |            80
         150 | Peter       | Tucker      | PTUCKER@example.com  | 011.44.1344.129268 | 1987-08-06 | SA_REP     | 10000.00 |           0.30 |        145 |            80
         151 | David       | Bernstein   | DBERNSTE@example.com | 011.44.1344.345268 | 1987-08-07 | SA_REP     |  9500.00 |           0.25 |        145 |            80
         152 | Peter       | Hall        | PHALL@example.com    | 011.44.1344.478968 | 1987-08-08 | SA_REP     |  9000.00 |           0.25 |        145 |            80
         153 | Christopher | Olsen       | COLSEN@example.com   | 011.44.1344.498718 | 1987-08-09 | SA_REP     |  8000.00 |           0.20 |        145 |            80
         154 | Nanette     | Cambrault   | NCAMBRAU@example.com | 011.44.1344.987668 | 1987-08-10 | SA_REP     |  7500.00 |           0.20 |        145 |            80
         155 | Oliver      | Tuvault     | OTUVAULT@example.com | 011.44.1344.486508 | 1987-08-11 | SA_REP     |  7000.00 |           0.15 |        145 |            80
         156 | Janette     | King        | JKING@example.com    | 011.44.1345.429268 | 1987-08-12 | SA_REP     | 10000.00 |           0.35 |        146 |            80
         157 | Patrick     | Sully       | PSULLY@example.com   | 011.44.1345.929268 | 1987-08-13 | SA_REP     |  9500.00 |           0.35 |        146 |            80
         158 | Allan       | McEwen      | AMCEWEN@example.com  | 011.44.1345.829268 | 1987-08-14 | SA_REP     |  9000.00 |           0.35 |        146 |            80
         159 | Lindsey     | Smith       | LSMITH@example.com   | 011.44.1345.729268 | 1987-08-15 | SA_REP     |  8000.00 |           0.30 |        146 |            80
         160 | Louise      | Doran       | LDORAN@example.com   | 011.44.1345.629268 | 1987-08-16 | SA_REP     |  7500.00 |           0.30 |        146 |            80
         161 | Sarath      | Sewall      | SSEWALL@example.com  | 011.44.1345.529268 | 1987-08-17 | SA_REP     |  7000.00 |           0.25 |        146 |            80
         162 | Clara       | Vishney     | CVISHNEY@example.com | 011.44.1346.129268 | 1987-08-18 | SA_REP     | 10500.00 |           0.25 |        147 |            80
         163 | Danielle    | Greene      | DGREENE@example.com  | 011.44.1346.229268 | 1987-08-19 | SA_REP     |  9500.00 |           0.15 |        147 |            80
         164 | Mattea      | Marvins     | MMARVINS@example.com | 011.44.1346.329268 | 1987-08-20 | SA_REP     |  7200.00 |           0.10 |        147 |            80
         165 | David       | Lee         | DLEE@example.com     | 011.44.1346.529268 | 1987-08-21 | SA_REP     |  6800.00 |           0.10 |        147 |            80
         166 | Sundar      | Ande        | SANDE@example.com    | 011.44.1346.629268 | 1987-08-22 | SA_REP     |  6400.00 |           0.10 |        147 |            80
         167 | Amit        | Banda       | ABANDA@example.com   | 011.44.1346.729268 | 1987-08-23 | SA_REP     |  6200.00 |           0.10 |        147 |            80
         168 | Lisa        | Ozer        | LOZER@example.com    | 011.44.1343.929268 | 1987-08-24 | SA_REP     | 11500.00 |           0.25 |        148 |            80
         169 | Harrison    | Bloom       | HBLOOM@example.com   | 011.44.1343.829268 | 1987-08-25 | SA_REP     | 10000.00 |           0.20 |        148 |            80
         170 | Tayler      | Fox         | TFOX@example.com     | 011.44.1343.729268 | 1987-08-26 | SA_REP     |  9600.00 |           0.20 |        148 |            80
         171 | William     | Smith       | WSMITH@example.com   | 011.44.1343.629268 | 1987-08-27 | SA_REP     |  7400.00 |           0.15 |        148 |            80
         172 | Elizabeth   | Bates       | EBATES@example.com   | 011.44.1343.529268 | 1987-08-28 | SA_REP     |  7300.00 |           0.15 |        148 |            80
         173 | Sundita     | Kumar       | SKUMAR@example.com   | 011.44.1343.329268 | 1987-08-29 | SA_REP     |  6100.00 |           0.10 |        148 |            80
         174 | Ellen       | Abel        | EABEL@example.com    | 011.44.1644.429267 | 1987-08-30 | SA_REP     | 11000.00 |           0.30 |        149 |            80
         175 | Alyssa      | Hutton      | AHUTTON@example.com  | 011.44.1644.429266 | 1987-08-31 | SA_REP     |  8800.00 |           0.25 |        149 |            80
         176 | Jonathon    | Taylor      | JTAYLOR@example.com  | 011.44.1644.429265 | 1987-09-01 | SA_REP     |  8600.00 |           0.20 |        149 |            80
         177 | Jack        | Livingston  | JLIVINGS@example.com | 011.44.1644.429264 | 1987-09-02 | SA_REP     |  8400.00 |           0.20 |        149 |            80
         178 | Kimberely   | Grant       | KGRANT@example.com   | 011.44.1644.429263 | 1987-09-03 | SA_REP     |  7000.00 |           0.15 |        149 |             0
         179 | Charles     | Johnson     | CJOHNSON@example.com | 011.44.1644.429262 | 1987-09-04 | SA_REP     |  6200.00 |           0.10 |        149 |            80
         180 | Winston     | Taylor      | WTAYLOR@example.com  | 650.507.9876       | 1987-09-05 | SH_CLERK   |  3200.00 |           0.00 |        120 |            50
         181 | Jean        | Fleaur      | JFLEAUR@example.com  | 650.507.9877       | 1987-09-06 | SH_CLERK   |  3100.00 |           0.00 |        120 |            50
         182 | Martha      | Sullivan    | MSULLIVA@example.com | 650.507.9878       | 1987-09-07 | SH_CLERK   |  2500.00 |           0.00 |        120 |            50
         183 | Girard      | Geoni       | GGEONI@example.com   | 650.507.9879       | 1987-09-08 | SH_CLERK   |  2800.00 |           0.00 |        120 |            50
         184 | Nandita     | Sarchand    | NSARCHAN@example.com | 650.509.1876       | 1987-09-09 | SH_CLERK   |  4200.00 |           0.00 |        121 |            50
         185 | Alexis      | Bull        | ABULL@example.com    | 650.509.2876       | 1987-09-10 | SH_CLERK   |  4100.00 |           0.00 |        121 |            50
         186 | Julia       | Dellinger   | JDELLING@example.com | 650.509.3876       | 1987-09-11 | SH_CLERK   |  3400.00 |           0.00 |        121 |            50
         187 | Anthony     | Cabrio      | ACABRIO@example.com  | 650.509.4876       | 1987-09-12 | SH_CLERK   |  3000.00 |           0.00 |        121 |            50
         188 | Kelly       | Chung       | KCHUNG@example.com   | 650.505.1876       | 1987-09-13 | SH_CLERK   |  3800.00 |           0.00 |        122 |            50
         189 | Jennifer    | Dilly       | JDILLY@example.com   | 650.505.2876       | 1987-09-14 | SH_CLERK   |  3600.00 |           0.00 |        122 |            50
         190 | Timothy     | Gates       | TGATES@example.com   | 650.505.3876       | 1987-09-15 | SH_CLERK   |  2900.00 |           0.00 |        122 |            50
         191 | Randall     | Perkins     | RPERKINS@example.com | 650.505.4876       | 1987-09-16 | SH_CLERK   |  2500.00 |           0.00 |        122 |            50
         192 | Sarah       | Bell        | SBELL@example.com    | 650.501.1876       | 1987-09-17 | SH_CLERK   |  4000.00 |           0.00 |        123 |            50
         193 | Britney     | Everett     | BEVERETT@example.com | 650.501.2876       | 1987-09-18 | SH_CLERK   |  3900.00 |           0.00 |        123 |            50
         194 | Samuel      | McCain      | SMCCAIN@example.com  | 650.501.3876       | 1987-09-19 | SH_CLERK   |  3200.00 |           0.00 |        123 |            50
         195 | Vance       | Jones       | VJONES@example.com   | 650.501.4876       | 1987-09-20 | SH_CLERK   |  2800.00 |           0.00 |        123 |            50
         196 | Alana       | Walsh       | AWALSH@example.com   | 650.507.9811       | 1987-09-21 | SH_CLERK   |  3100.00 |           0.00 |        124 |            50
         197 | Kevin       | Feeney      | KFEENEY@example.com  | 650.507.9822       | 1987-09-22 | SH_CLERK   |  3000.00 |           0.00 |        124 |            50
         198 | Donald      | OConnell    | DOCONNEL@example.com | 650.507.9833       | 1987-09-23 | SH_CLERK   |  2600.00 |           0.00 |        124 |            50
         199 | Douglas     | Grant       | DGRANT@example.com   | 650.507.9844       | 1987-09-24 | SH_CLERK   |  2600.00 |           0.00 |        124 |            50
         200 | Jennifer    | Whalen      | JWHALEN@example.com  | 515.123.4444       | 1987-09-25 | AD_ASST    |  4400.00 |           0.00 |        101 |            10
         201 | Michael     | Hartstein   | MHARTSTE@example.com | 515.123.5555       | 1987-09-26 | MK_MAN     | 13000.00 |           0.00 |        100 |            20
         202 | Pat         | Fay         | PFAY@example.com     | 603.123.6666       | 1987-09-27 | MK_REP     |  6000.00 |           0.00 |        201 |            20
         203 | Susan       | Mavris      | SMAVRIS@example.com  | 515.123.7777       | 1987-09-28 | HR_REP     |  6500.00 |           0.00 |        101 |            40
         204 | Hermann     | Baer        | HBAER@example.com    | 515.123.8888       | 1987-09-29 | PR_REP     | 10000.00 |           0.00 |        101 |            70
         205 | Shelley     | Higgins     | SHIGGINS@example.com | 515.123.8080       | 1987-09-30 | AC_MGR     | 12000.00 |           0.00 |        101 |           110
         206 | William     | Gietz       | WGIETZ@example.com   | 515.123.8181       | 1987-10-01 | AC_ACCOUNT |  8300.00 |           0.00 |        205 |           110
         108 | Nancy       | Greenberg   | NGREENBE@example.com | 515.999.4569       | 1987-06-25 | FI_MGR     | 12000.00 |           0.00 |        101 |           100
         109 | Daniel      | Faviet      | DFAVIET@example.com  | 515.999.4169       | 1987-06-26 | FI_ACCOUNT |  9000.00 |           0.00 |        108 |           100
         110 | John        | Chen        | JCHEN@example.com    | 515.999.4269       | 1987-06-27 | FI_ACCOUNT |  8200.00 |           0.00 |        108 |           100
         111 | Ismael      | Sciarra     | ISCIARRA@example.com | 515.999.4369       | 1987-06-28 | FI_ACCOUNT |  7700.00 |           0.00 |        108 |           100
         112 | Jose Manuel | Urman       | JMURMAN@example.com  | 515.999.4469       | 1987-06-29 | FI_ACCOUNT |  7800.00 |           0.00 |        108 |           100
         113 | Luis        | Popp        | LPOPP@example.com    | 515.999.4567       | 1987-06-30 | FI_ACCOUNT |  6900.00 |           0.00 |        108 |           100
         125 | Julia       | Nayer       | JNAYER@example.com   | 650.999.1214       | 1987-07-12 | ST_CLERK   |  3200.00 |           0.00 |        120 |            50
         126 | Irene       | Mikkilineni | IMIKKILI@example.com | 650.999.1224       | 1987-07-13 | ST_CLERK   |  2700.00 |           0.00 |        120 |            50
         127 | James       | Landry      | JLANDRY@example.com  | 650.999.1334       | 1987-07-14 | ST_CLERK   |  2400.00 |           0.00 |        120 |            50
         128 | Steven      | Markle      | SMARKLE@example.com  | 650.999.1434       | 1987-07-15 | ST_CLERK   |  2200.00 |           0.00 |        120 |            50
         129 | Laura       | Bissot      | LBISSOT@example.com  | 650.999.5234       | 1987-07-16 | ST_CLERK   |  3300.00 |           0.00 |        121 |            50
         130 | Mozhe       | Atkinson    | MATKINSO@example.com | 650.999.6234       | 1987-07-17 | ST_CLERK   |  2800.00 |           0.00 |        121 |            50
         131 | James       | Marlow      | JAMRLOW@example.com  | 650.999.7234       | 1987-07-18 | ST_CLERK   |  2500.00 |           0.00 |        121 |            50
         132 | TJ          | Olson       | TJOLSON@example.com  | 650.999.8234       | 1987-07-19 | ST_CLERK   |  2100.00 |           0.00 |        121 |            50
DEPARTMENT_IDDEPARTMENT_NAMEMANAGER_IDLOCATION_ID
10Administration2001700
20Marketing2011800
30Purchasing1141700
40Human Resources2032400
50Shipping1211500
60IT1031400
70Public Relations2042700
80Sales1452500
90Executive1001700
100Finance1081700
110Accounting2051700
120Treasury-1700
130Corporate Tax-1700
140Control And Credit-1700
150Shareholder Services-1700
160Benefits-1700
170Manufacturing-1700
180Construction-1700
190Contracting-1700
200Operations-1700
210IT Support-1700
220NOC-1700
230IT Helpdesk-1700
240Government Sales-1700
250Retail Sales-1700
260Recruiting-1700
270Payroll-1700

IN operator

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

SELECT first_name, last_name,department_id 
FROM employees 
WHERE department_id IN 
(SELECT DEPARTMENT_ID FROM departments 
WHERE location_id=1800);

Result:

------------+-----------+---------------
 Michael    | Hartstein |            20
 Pat        | Fay       |            20
(2 rows)

From the figure below, List products with order quantities greater than 100.

Query:

SELECT ProductName
  FROM Product 
 WHERE Id IN (SELECT ProductId 
                FROM OrderItem
               WHERE Quantity > 100)

EXISTS operator

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.

From sample tables, The following query finds employees (employee_id, first_name, last_name, job_id, department_id) from employees table who have at least one person reporting to them.

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);

Result:

 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
		 ............
18 rows in set (0.02 sec)

Row Subqueries

A row subquery is a subquery that returns a single row and more than one column value. You can use = , >, <, >=, <=, <>, !=, <=> comparison operators.

From sample tables, In the following examples, queries shows different result according to above conditions :

Query:

SELECT first_name 
FROM employees 
WHERE ROW(department_id, manager_id) = 
(SELECT department_id, manager_id 
FROM departments 
WHERE location_id = 1800);

Result:

 first_name
------------
 Pat
(1 row)

Query:

SELECT first_name 
FROM employees
WHERE ROW(department_id, manager_id) = 
(SELECT department_id, manager_id 
FROM departments 
WHERE location_id = 2800);

Result:

 first_name
------------
(0 rows)

Query:

SELECT first_name 
FROM employees 
WHERE ROW(department_id, manager_id) = 
(SELECT department_id, manager_id 
FROM departments 
WHERE location_id = 1700);

Result:

ERROR: more than one row returned by a subquery used as an expression

FROM clause

From parch and posey database, If we want to get average of channels of each account.

SELECT AVG(t1.num_channels),
       t1.name
FROM
  (SELECT COUNT(*) AS num_channels, a.name
  FROM web_events AS w
  JOIN accounts AS a
  ON w.account_id = a.id
  GROUP BY w.channel, a.id, a.name) AS t1

SELECT clause

From the figure below, List all customers with their total number of orders.

Query:

SELECT FirstName, LastName, 
       OrderCount = (SELECT COUNT(O.Id) FROM [Order] O WHERE O.CustomerId = C.Id)
  FROM Customer C 

References: