GAZAR

Principal Engineer | Mentor

How to Select and SubQuery in SQL Syntax

How to Select and SubQuery in SQL Syntax

Select all records from the Customers table:

SELECT * FROM Customers;

Select all customers from Mexico:

SELECT * FROM Customers
WHERE Country='Mexico';

The following SQL statement lists the number of customers in each country:

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;

The following SQL statement lists the number of customers in each country. Only include countries with more than 5 customers:

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

or Alternatively

SELECT * from 
(SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country) 
WHERE Expr1000 > 5