0% found this document useful (0 votes)
43 views4 pages

Joins Chatgpt Practice

The document provides examples of 10 SQL queries: 1) Joins two tables to retrieve orders with customer names and dates. 2) Retrieves customers who have not placed orders using left join and null filtering. 3) Joins three tables to show employee names, departments, and projects. 4) Self joins an employee table to show employee and manager names. 5) Joins two tables and uses aggregation to calculate total revenue by order.

Uploaded by

Priyansh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

Joins Chatgpt Practice

The document provides examples of 10 SQL queries: 1) Joins two tables to retrieve orders with customer names and dates. 2) Retrieves customers who have not placed orders using left join and null filtering. 3) Joins three tables to show employee names, departments, and projects. 4) Self joins an employee table to show employee and manager names. 5) Joins two tables and uses aggregation to calculate total revenue by order.

Uploaded by

Priyansh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

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 Customers.customer_id, Customers.customer_name, Customers.customer_email


FROM Customers
LEFT JOIN Orders ON Customers.customer_id = Orders.customer_id
WHERE Orders.order_id IS NULL;

Starting from the "Customers" table is essential because we want to retrieve


information about
customers who have not placed any orders. The process involves using a LEFT JOIN to
combine the
"Customers" table with the "Orders" table,
and then we filter the result to find the customers without any orders.

3.Consider three tables: "Employees," "Departments," and "Projects," with the


following columns:

Employees: employee_id, employee_name, department_id


Departments: department_id, department_name
Projects: project_id, project_name, department_id
Write a SQL query to retrieve the names of employees along with the names of the
departments they
belong to and the projects they are working on, if any.

SELECT e.employee_name AS EmployeeName,


d.department_name AS DepartmentName,
p.project_name AS ProjectName
FROM Employees e
LEFT JOIN Departments d ON e.department_id = d.department_id
LEFT JOIN Projects p ON e.department_id = p.department_id;

4.Consider a table called "Employees" with columns:


Employees: employee_id, employee_name, manager_id
Write a SQL query to retrieve the names of employees along with the names of their
managers.

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;

or(using window functions)


SELECT DISTINCT
order_id,
SUM(product_price * quantity) OVER (PARTITION BY order_id) AS total_revenue
FROM OrderItems
JOIN Products ON OrderItems.product_id = Products.product_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.

SELECT oi.order_id, SUM(oi.quantity) AS Total


FROM OrderItems oi
GROUP BY oi.order_id
HAVING Total > 50;

7.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 placed by customers from the United
States.

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.

SELECT o.order_id, o.order_date, c.customer_name


FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id;

9.Consider a table called "Employees" with columns:


Employees: employee_id, employee_name, manager_id
Write a SQL query to find the names of employees along with the names of their
managers and the
total number of employees each manager supervises.

SELECT e1.employee_name,e2.employee_name AS manager_name ,COUNT(e2.manager_id) AS


TotalEmployees
FROM Employee e1
INNER JOIN Employee e2 ON e1.employee_id=e2.manager_id
GROUP BY e2,employee_id,e2.employee_name

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.

SELECT o.order_id, o.order_date, c.customer_name


FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id
WHERE o.customer_id IN (
SELECT customer_id
FROM Orders
GROUP BY customer_id
HAVING COUNT(order_id) > 1
);

You might also like