The SQL subquery syntax
There is no general syntax; subqueries are regular queries placed inside parenthesis.
Subqueries can be used in different ways and at different locations inside a query:
Here is an subquery with the IN operator
- SELECT column–names
- FROM table–name1
- WHERE value IN (SELECT column–name
- FROM table–name2
- WHERE condition)
Subqueries can also assign column values for each record:
- SELECT column1 = (SELECT column–name FROM table–name WHERE condition),
- column–names
- FROM table–name
- WEHRE condition
SQL Subquery Examples
Problem: List products with order quantities greater than 100.
- SELECT ProductName
- FROM Product
- WHERE Id IN (SELECT ProductId
- FROM OrderItem
- WHERE Quantity > 100)
Results: 12 records
PoductName |
---|
Guaraná Fantástica |
Schoggi Schokolade |
Chartreuse verte |
Jack’s New England Clam Chowder |
Rogede sild |
Manjimup Dried Apples |
Perth Pasties |
![]() |
SQL Subquery Examples
Problem: List all customers with their total number of orders
- SELECT FirstName, LastName,
- OrderCount = (SELECT COUNT(O.Id) FROM [Order] O WHERE O.CustomerId = C.Id)
- FROM Customer C
This is a correlated subquery because the subquery references the enclosing query (i.e. the C.Id in the WHERE clause).
Results: 91 records
FirstName | LastName | OrderCount |
---|---|---|
Maria | Anders | 6 |
Ana | Trujillo | 4 |
Antonio | Moreno | 7 |
Thomas | Hardy | 13 |
Christina | Berglund | 18 |
Hanna | Moos | 7 |
Frédérique | Citeaux | 11 |
Martín | Sommer | 3 |
![]() |
핑백: SSCN BKN 2021 Kabupaten Mukomuko
핑백: mơ thấy cóc đánh con gì
핑백: cây trồng trong phòng làm việc
핑백: mơ thấy mít chín đánh con gì
핑백: RPA in software testing
핑백: tai game dua ngua
핑백: nằm mơ thấy đứt tay chảy máu
핑백: mơ thấy bàn thờ
핑백: chup anh thay ma
핑백: Tyson vs Jones jr live stream online
핑백: cbd oil buy canada
핑백: Azure Devops
핑백: 안전공원
핑백: incredit patēriņa kredīts
핑백: www.manausa.com
핑백: nằm mơ thấy phật bà quan âm
핑백: mơ thấy đi chợ
핑백: mơ thấy chìa khóa
핑백: mơ cá sấu đánh con gì
핑백: nằm mơ thấy bướm
핑백: michigan state cornhole
핑백: mơ thấy chim bồ câu đánh con gì
핑백: mơ thấy chiến tranh
핑백: site
tinder app , tinder sign up https://tinderdatingsiteus.com/
핑백: Maytag UXT5230AYS manuals
핑백: Arcam Audio tuners manuals
핑백: card dumps
핑백: คอนโดปูนา
핑백: Paket Wisata Bali