SQL Exercices
This is a tutorial for SQL.
You can use the following online platform: DB-Fiddle.
The database is available at the following link.
Questions
Write a command to get the
customer_id,first_nameandorder_idfrom the tablescustomersandorders. It should be ordered and joined by customer_id.Change the
JOINwithLEFT JOIN. What do you observe ?Choose the correct join clause to display all information from the
ordersandshipperstables where there is a matchDisplay all records from the
customerstable along with the corresponding information, where available, from theorderstable using a join clauseThe
orderstable contains ashipper_idcolumn which identifies a supplier for each order.
This identifier corresponds to a supplier name accessible via the shippers table.
Modify the query in Question 1 (with the LEFT JOIN added) to display :
- column 1, the customer ID, for all customers
- column 2, the customer’s first name
- column 3, the order ID (NULL for customers who haven’t ordered anything)
- column 4, the supplier name (if documented and if an order has been placed)
- Consider the following query containing an “explicit” cross-join:
SELECT
c.first_name AS customer,
p.name AS product
FROM customers c
CROSS JOIN products p
ORDER BY c.first_name
Write a query without CROSS JOIN but displaying the same result.
- In the
customerstable, each customer has loyalty points.
There are three categories of customer:
Silverfor points < 2000,Goldfor 2000 <= points <= 3000,Platinumfor points > 3000.
Write a query displaying :
- column 1, the customer ID
- column 2, the customer’s first name
- column 3, points
- column 4, category
The rows should be ordered alphabetically by first name.
What are the
customer_iddetails of customers who have never placed an order?Ilene Dowson’s order has been delivered. Change its status in the ‘orders’ table, and check it.
Which products are ordered most often? Rank them in descending order by displaying the product identifier, its name and the number of times it has been ordered