The document contains SQL queries to retrieve customer and order data from a database. It includes queries to display order details with customer names, find orders over $100, list customers without orders, calculate average order total, and more.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
10 views
Practice SQL Queries
The document contains SQL queries to retrieve customer and order data from a database. It includes queries to display order details with customer names, find orders over $100, list customers without orders, calculate average order total, and more.
FROM orders o JOIN customers c ON o.customer_id = c.customer_id; --2. Retrieve the orders with a total amount greater than $100. SELECT * FROM orders WHERE total_amount > 100.00; --3. List customers who have not placed any orders. SELECT c.* FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id WHERE o.order_id IS NULL; --4. Display the average total amount of orders. SELECT AVG(total_amount) AS average_total_amount FROM orders; --5.Retrieve the customer names along with the total number of orders they have placed. SELECT c.first_name, c.last_name, COUNT(o.order_id) AS total_orders FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.first_name, c.last_name; --6. Display the orders placed in the year 2022, including customer information. SELECT o.order_id, c.first_name, c.last_name, o.order_date, o.total_amount FROM orders o JOIN customers c ON o.customer_id = c.customer_id WHERE strftime('%Y', o.order_date) = '2022'; --7. Find the customer with the highest total amount of orders. SELECT c.customer_id, c.first_name, c.last_name, MAX(o.total_amount) AS highest_total_amount FROM customers c JOIN orders o ON c.customer_id = o.customer_id; --8. To find the customer who has placed the 3rd highest amount of orders --Method 1 SELECT c.customer_id, c.first_name, c.last_name, SUM(o.total_amount) AS total_order_amount FROM customers c JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.first_name, c.last_name ORDER BY total_order_amount DESC LIMIT 1 OFFSET 2; -- Method 2 using CTE(Common Table Expression) WITH RankedOrders AS ( SELECT c.customer_id, c.first_name, c.last_name, SUM(o.total_amount) AS total_order_amount, ROW_NUMBER() OVER (ORDER BY SUM(o.total_amount) DESC) AS order_rank FROM customers c JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.first_name, c.last_name ) SELECT customer_id, first_name, last_name, total_order_amount FROM RankedOrders WHERE order_rank = 3; --9. Find customers who have placed orders with a total amount greater than the average total amount of all orders. SELECT c.customer_id, c.first_name, c.last_name FROM customers c WHERE customer_id IN ( SELECT o.customer_id FROM orders o GROUP BY o.customer_id HAVING SUM(o.total_amount) > (SELECT AVG(total_amount) FROM orders) ); -- 10. Retrieve the customer details along with the total number of orders each customer has placed. SELECT c.customer_id, c.first_name, c.last_name, (SELECT COUNT(*) FROM orders o WHERE o.customer_id = c.customer_id) AS total_orders FROM customers c;