SQL - Data Query Language Exercises Part 2
1. Write a query to display the first names of customers, sorted in ascending
order.
SELECT first_name
FROM Customers
ORDER BY first_name ASC;
2. Write a query to display the first names of customers, sorted in descending
order.
SELECT first_name
FROM Customers
ORDER BY first_name DESC;
3. Write a query to display the first and last names of customers, sorted by
first name in descending order, and if the first names are the same, sorted
by their last names in descending order.
SELECT first_name, last_name
FROM Customers
ORDER BY first_name DESC, last_name DESC;
4. Write a query to display first names and ages of customers, sorted by their
age from earlier to older, and if the ages are the same, sorted by their first
names in ascending order.
SELECT first_name, age
FROM Customers
ORDER BY age ASC, first_name ASC;
5. Write a query to display unique countries of customers, sorted
alphabetically.
SELECT DISTINCT country
FROM Customers
ORDER BY country ASC;
6. Write a query to display unique items and their amounts, sorted by amount
in descending order, and if the amounts are the same, sorted by the items
in ascending order
SELECT DISTINCT item, amount
FROM Orders
ORDER BY amount DESC, item ASC;
7. Write a query to display the first names of the first 10 customers.
SELECT first_name
FROM Customers
LIMIT 10;
8. Write a query to display the first and last names of customers and limit the
results to the first 5 rows.
SELECT first_name, last_name
FROM Customers
LIMIT 5;
9. Write a query to display all details of customers and limit the results to the
first 15 rows.
SELECT *
FROM Customers
LIMIT 15;
10. Write a query to display unique countries of customers and limit the
results to the first 4 rows.
SELECT DISTINCT country
FROM Customers
LIMIT 4;
11. Write a query to display unique items and their amounts and limit the
results to the first 10 rows.
SELECT DISTINCT item, amount
FROM Orders
LIMIT 10;
12. Write a query to display the IDs of customers and limit the results to 5
rows starting from row number 7.
SELECT customer_id
FROM Customers
LIMIT 5
OFFSET 6;
13. Write a query to display IDs and first names of customers, and limit the
results to 10 rows, starting from row number 8.
SELECT customer_id, first_name
FROM Customers
LIMIT 10
OFFSET 7;
14. Write a query to display all details of customers and limit the results to 15
rows starting from row number 20.
SELECT *
FROM Customers
LIMIT 15
OFFSET 19;
15. Write a query to display unique first names of customers and their
countries and limit the results to 7 rows starting from row number 9.
SELECT DISTINCT first_name, country
FROM Customers
LIMIT 7
OFFSET 8;
16. Write a query to display the first and last names of customers from
Canada.
SELECT first_name, last_name
FROM Customers
WHERE country = 'Canada';
17. Write a query to display unique first names of customers and their
countries from Australia.
SELECT DISTINCT first_name, country
FROM Customers
WHERE country = 'Australia';
18. Write a query to display all details of a customer whose ID is 1.
SELECT *
FROM Customers
WHERE customer_id = 1;
19. Write a query to display all details of customers, excluding the customer
with customerID equal to 1.
SELECT *
FROM Customers
WHERE customer_id <> 1;
20. Write a query to display unique items and their amounts where the
amount is greater than 100.
SELECT DISTINCT item, amount
FROM Orders
WHERE amount > 100;
21. Write a query to display unique items and their amounts where the
amount is greater than or equal to 100.
SELECT DISTINCT item, amount
FROM Orders
WHERE amount >= 100;
22. Write a query to display unique items and their amounts where the
amount is smaller than 100.
SELECT DISTINCT item, amount
FROM Orders
WHERE amount < 100;
23. Write a query to display unique items and their amounts where the
amount is smaller than or equal to 100.
SELECT DISTINCT item, amount
FROM Orders
WHERE amount <= 100;