SQL Examples
SQL Examples
2. select the "CustomerName" and "City" columns from the "Customers" table:
SELECT CustomerName,City FROM Customers;
3. selects only the DISTINCT values from the "Country" column in the
"Customers" table
SELECT DISTINCT Country FROM Customers;
The following SQL statement lists the number of different (distinct) customer
countries:
SELECT COUNT(DISTINCT Country) FROM Customers;
4. The following SQL statement selects all the customers from the country "Mexico",
in the "Customers" table:
SELECT * FROM Customers
WHERE Country='Mexico';
SQL requires single quotes around text values (most database systems will also allow
double quotes).
However, numeric fields should not be enclosed in quotes:
SELECT * FROM Customers
WHERE CustomerID=1;
Select all records where the City column has the value "Berlin".
SELECT * FROM Customers
Where city="Berlin";
5. The following SQL statement selects all fields from "Customers" where country is
"Germany" AND city is "Berlin":
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
The following SQL statement selects all fields from "Customers" where city is "Berlin"
OR "München":
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
The following SQL statement selects all fields from "Customers" where country is NOT
"Germany":
SELECT * FROM Customers
WHERE NOT Country='Germany';
The following SQL statement selects all fields from "Customers" where country is
"Germany" AND city must be "Berlin" OR "München" (use parenthesis to form
complex expressions):
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München');
Select all records from the Customers table, sort the result alphabetically by the column
City.
SELECT * FROM Customers
ORDER BY City;
Select all records from the Customers table, sort the result reversed alphabetically by
the column City
SELECT * FROM Customers
ORDER BY City DESC;
Select all records from the Customers table, sort the result alphabetically, first by the
column Country, then, by the column City.
SELECT * FROM Customers
ORDER BY Country, City;
Select all records from the Customers where the PostalCode column is empty.
Delete all the records from the Customers table where the Country value is 'Norway'.
DELETE FROM Customers
WHERE Country=’Norway’;
The following SQL statement selects the first three records from the "Customers" table,
where the country is "Germany":
SELECT TOP 3 * FROM Customers
WHERE Country='Germany';
Use the MIN function to select the record with the smallest value of the Price column.
SELECT MIN(Price)
FROM Products;
Use the correct function to return the numbers of records that have the Price value set
to 18.
SELECT COUNT(*)
FROM Products
WHERE Price = 18;
The following SQL statement selects all customers that are from the same countries as
the suppliers:
SELECT * FROM Customers
WHERE Country IN (SELECT Country FROM Suppliers);
Use the IN operator to select all the records where Country is either "Norway" or
"France".
SELECT*FROM Customers
WHERE Country IN (‘Norway’, ‘France’);
The following SQL statement selects all products with a ProductName NOT
BETWEEN Carnarvon Tigers and Mozzarella di Giovanni:
SELECT * FROM Products
WHERE ProductName NOT BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di
Giovanni'
ORDER BY ProductName;
When displaying the Customers table, make an ALIAS of the PostalCode column, the
column should be called Pno instead.
SELECT CustomerName, Address, PostalCode AS Pno
FROM Customers;
Insert the missing parts in the JOIN clause to join the two tables Orders and Customers,
using the CustomerID field in both tables as the relationship between the two tables.
SELECT*FROM Orders
LEFT JOIN Customers
ON Orders.CustomerID=Customers.CustomerID
The following SQL statement selects all orders with customer and shipper
information:
The following SQL statement will select all customers, and any orders they might have:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
Choose the correct JOIN clause to select all the records from the Customers table plus
all the matches in the Orders table.
SELECT*FROM Orders
RIGHT JOIN Customers on Orders.CustomerID=Customers.CustomerID;
The following SQL statement returns the German cities (duplicate values also) from
both the "Customers" and the "Suppliers" table:
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION ALL
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
The following SQL statement lists the number of customers in each country, sorted
high to low:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
The following SQL statement lists the number of customers in each country, sorted
high to low (Only include countries with more than 5 customers):
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
The following SQL will order the customers by City. However, if City is NULL, then
order by Country:
SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
WHEN City IS NULL THEN Country
ELSE City
END);