Queries
Queries
SQL IN Operator
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
The following SQL statement selects all customers that are located in "Germany", "France" or
"UK":
The following SQL statement selects all customers that are NOT located in "Germany", "France"
or "UK":
The BETWEEN operator selects values within a given range. The values can be numbers, text,
or dates.
The BETWEEN operator is inclusive: begin and end values are included.
BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
To display the products outside the range of the previous example, use NOT BETWEEN:
The following SQL statement selects all products with a price between 10 and 20. In addition; do
not show products with a CategoryID of 1,2, or 3:
The following SQL statement selects all products with a ProductName between Carnarvon
Tigers and Mozzarella di Giovanni:
The following SQL statement selects all products with a ProductName between Carnarvon
Tigers and Chef Anton's Cajun Seasoning:
SELECT * FROM Products
WHERE ProductName BETWEEN "Carnarvon Tigers" AND "Chef Anton's Cajun Seasoning"
ORDER BY ProductName;
The following SQL statement selects all products with a ProductName not between Carnarvon
Tigers and Mozzarella di Giovanni:
The GROUP BY statement groups rows that have the same values into summary rows, like "find
the number of customers in each country".
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Below is a selection from the "Customers" table in the Northwind sample database:
The following SQL statement lists the number of customers in each country:
The following SQL statement lists the number of customers in each country, sorted high to low:
The following SQL statement lists the number of customers in each country. Only include
countries with more than 5 customers:
The following SQL statement lists the number of customers in each country, sorted high to low
(Only include countries with more than 5 customers):