Joins Chatgpt Practice
Joins Chatgpt Practice
Consider two tables, "Orders" and "Customers," with the following columns:
Orders: order_id, customer_id, order_date, order_total
Customers: customer_id, customer_name, customer_email
Write a SQL query to retrieve all orders along with their corresponding customer
names and order dates.
SELECT
Orders.order_id,Orders.order_date,Customers.Customers_name,Customers.customers_emai
l
FROM Orders
JOIN Customers ON Orders.customer_id=Customers.customer_id
2.Using the same tables as above, write a SQL query to retrieve all customers who
have not placed any orders.
SELECT e1.employee_id,e2.manager_id
FROM Employee e1
INNER JOIN Employee e2 ON e1.employee_id=e2.manager_id;
In MySQL, the syntax for a self join requires using the INNER JOIN or simply JOIN
keyword;
there is no SELF JOIN keyword.
5.Consider two tables, "Products" and "OrderItems," with the following columns:
Products: product_id, product_name, product_price
OrderItems: order_item_id, order_id, product_id, quantity
Write a SQL query to calculate the total revenue generated by each order.
SELECT oi.order_id,
SUM(p.product_price * oi.quantity) AS total_revenue
FROM OrderItems oi
JOIN Products p ON oi.product_id = p.product_id
GROUP BY oi.order_id;
6.Using the same tables as above, write a SQL query to find the order IDs and the
total quantity of products for
orders where the total quantity exceeds 50.
7.Consider two tables, "Customers" and "Orders," with the following columns:
SELECT oi.order_id,oi.order_date,c.customer_name
FROM Orders o
JOIN Customers c ON o.customer_id=c.customer_id
WHERE c.cutomer_country='United State'
8.Consider two tables, "Customers" and "Orders," with the following columns:
Customers: customer_id, customer_name, customer_country
Orders: order_id, customer_id, order_date
Write a SQL query to retrieve all orders along with their corresponding customer
names
and order dates. Use aliases for the table names.
10.Consider two tables, "Orders" and "Customers," with the following columns:
Orders: order_id, customer_id, order_date, order_total
Customers: customer_id, customer_name, customer_email
Write a SQL query to retrieve all orders along with their corresponding customer
names and order dates,
but only for customers who have placed more than one order.