JOINS
JOINS
PRACTICE 1
Questions:-
-- Question 1: Retrieve all employees with their department names
-- Question 2: Retrieve employees and their department names, including those without a
Department
-- Question 3: Retrieve the total number of employees in each department
-- Question 1: Retrieve all employees with their department names
-- Question 2: Retrieve employees and their department names, including those without a
Department
Questions:-
-- Question 1: Retrieve employees with their department names and the projects they are working
on
-- Question 2: Retrieve the total number of employees and the total number of projects in each
department
-- Question 1: Retrieve employees with their department names and the projects they are working
on
-- Question 2: Retrieve the total number of employees and the total number of projects in each
department
Questions:-
1.List all orders with customer details (first name, last name) for each order.
2.Find the total amount spent by each customer. Include customers who haven't placed any orders.
3.List the customer names and email addresses for those who have placed orders in the year 2023.
4.Identify customers who have not placed any orders.
5.List the order details (order ID, order date, total amount) along with customer names for orders
placed in February 2023.
6.Find the average order amount for each customer. Include customers who haven't placed any
orders.
1.List all orders with customer details (first name, last name) for each order.
2.Find the total amount spent by each customer. Include customers who haven't placed any
orders.
3.List the customer names and email addresses for those who have placed orders in the year 2023.
5.List the order details (order ID, order date, total amount) along with customer names for orders
placed in February 2023.
6.Find the average order amount for each customer. Include customers who haven't placed any
orders.
Questions:-
1.Question: Retrieve the names of employees along with their corresponding department names.
2.Question: List all departments and the names of employees (if any) in each department.
3.Question: Find the total salary of each department.
4.Question: Retrieve the names of employees, their department names, and the location of each
department.
5.Question: List the names of employees in the IT department who earn more than $60,000.
6.Question: Find pairs of employees who have the same manager (assuming a manager is also an
employee in the same table).
1.Question: Retrieve the names of employees along with their corresponding department names.
SELECT employees.employee_name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;
Left Join with Null Values:
2.Question: List all departments and the names of employees (if any) in each department.
4.Question: Retrieve the names of employees, their department names, and the location of each
department.
5.Question: List the names of employees in the IT department who earn more than $60,000.
SELECT employees.employee_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id
WHERE departments.department_name = 'IT' AND employees.salary > 60000;
6.Question: Find pairs of employees who have the same manager (assuming a manager is also an
employee in the same table).
Questions:-
1.Question: Display the names of employees along with the names of their managers. Assume that
the manager is also an employee in the same table and is identified by the emp_manager_id
column, which refers to the emp_id of the manager.
2.Question: Generate all possible combinations of employee names and department names,
regardless of whether there is a match between them.
3.Question: Display the names of employees who have a salary greater than the average salary of
employees in the IT department.
4.Question: List the names of employees and their corresponding department locations.
5.Question: Show a list of all employees and their salaries along with the department information. If
an employee is not assigned to any department, display "N/A" for department information.
6.Question: Display the department names and the names of employees belonging to those
departments. Include departments with no employees, displaying "No employees" for such cases.
7.Question: List all employees and their departments along with the department information. If an
employee or department has no match, display "Unassigned" for employees or "No employees" for
departments.
1.Question: Display the names of employees along with the names of their managers. Assume that
the manager is also an employee in the same table and is identified by the emp_manager_id
column, which refers to the emp_id of the manager.
2.Question: Generate all possible combinations of employee names and department names,
regardless of whether there is a match between them.
3.Question: Display the names of employees who have a salary greater than the average salary of
employees in the IT department.
SELECT emp_name
FROM employees
INNER JOIN departments ON employees.emp_department = departments.dept_name
WHERE emp_salary > (SELECT AVG(emp_salary) FROM employees WHERE emp_department = 'IT');
4.Question: List the names of employees and their corresponding department locations.
5.Question: Show a list of all employees and their salaries along with the department information.
If an employee is not assigned to any department, display "N/A" for department information.
7.Question: List all employees and their departments along with the department information. If an
employee or department has no match, display "Unassigned" for employees or "No employees"
for departments.
Questions:-
2.List the total working hours for each employee on a specific date:
SELECT
Employee.EmployeeID,
Employee.FirstName,
Employee.LastName,
Attendance.AttendanceDate,
SUM(DATEDIFF(MINUTE, Attendance.StartTime, Attendance.EndTime)) AS TotalWorkingMinutes
FROM Employee
LEFT JOIN Attendance ON Employee.EmployeeID = Attendance.EmployeeID
WHERE Attendance.AttendanceDate = '2023-01-01'
GROUP BY Employee.EmployeeID, Employee.FirstName, Employee.LastName,
Attendance.AttendanceDate;
3. Retrieve a list of employees who have not recorded attendance on a given date:
SELECT
Employee.EmployeeID,
Employee.FirstName,
Employee.LastName
FROM Employee
LEFT JOIN Attendance ON Employee.EmployeeID = Attendance.EmployeeID
WHERE Attendance.EmployeeID IS NULL OR Attendance.AttendanceDate <> '2023-01-01';
SELECT
Employee.EmployeeID,
Employee.FirstName,
Employee.LastName,
AVG(DATEDIFF(MINUTE, Attendance.StartTime, Attendance.EndTime)) AS
AverageWorkingMinutes
FROM Employee
LEFT JOIN Attendance ON Employee.EmployeeID = Attendance.EmployeeID
GROUP BY Employee.EmployeeID, Employee.FirstName, Employee.LastName;
5. Retrieve a list of employees who have attended on all recorded dates:
SELECT
Employee.EmployeeID,
Employee.FirstName,
Employee.LastName
FROM Employee
JOIN Attendance ON Employee.EmployeeID = Attendance.EmployeeID
GROUP BY Employee.EmployeeID, Employee.FirstName, Employee.LastName
HAVING COUNT(DISTINCT Attendance.AttendanceDate) = (SELECT COUNT(DISTINCT
AttendanceDate) FROM Attendance);
SELECT
e1.EmployeeID AS Employee1ID,
e1.FirstName AS Employee1FirstName,
e1.LastName AS Employee1LastName,
e2.EmployeeID AS Employee2ID,
e2.FirstName AS Employee2FirstName,
e2.LastName AS Employee2LastName,
a.AttendanceDate
FROM Employee e1
JOIN Employee e2 ON e1.EmployeeID < e2.EmployeeID
JOIN Attendance a ON e1.EmployeeID = a.EmployeeID AND e2.EmployeeID = a.EmployeeID AND
e1.AttendanceDate = a.AttendanceDate;
7. List employees and the total number of days they have attended:
SELECT
Employee.EmployeeID,
Employee.FirstName,
Employee.LastName,
COUNT(DISTINCT Attendance.AttendanceDate) AS TotalAttendanceDays
FROM Employee
LEFT JOIN Attendance ON Employee.EmployeeID = Attendance.EmployeeID
GROUP BY Employee.EmployeeID, Employee.FirstName, Employee.LastName;
PRACTISE 7
-- Create ORDERS table
CREATE TABLE ORDERS (
order_id INT PRIMARY KEY,
customer_name VARCHAR(255),
order_date DATE,
total_amount DECIMAL(10, 2)
);
1.Retrieve all orders with customer names and corresponding transaction amounts.
2.List all transactions with transaction details, including customer names and bank names.
4.Retrieve the details of orders that have transactions with amounts greater than $150.
5.List the banks with their total transaction amounts, ordered from the highest total amount to the
lowest.
1. Retrieve all orders with customer names and corresponding transaction amounts.
SELECT o.order_id, o.customer_name, t.amount
FROM ORDERS o
JOIN TRANSACTIONS t ON o.order_id = t.order_id;
2. List all transactions with transaction details, including customer names and bank names.
4. Retrieve the details of orders that have transactions with amounts greater than $150.
5. List the banks with their total transaction amounts, ordered from the highest total amount
to the lowest.
3. Find the employees who earn more than the average salary in their department.
7.Total budget for each department: Retrieve the total budget for each department.
8.Employees with salaries above the average: Find the employees who earn more than the average
salary in their department.
3. Find the employees who earn more than the average salary in their department.
8.Employees with salaries above the average: Find the employees who earn more than the
average salary in their department.
Questions:-
3.Retrieve the details of passengers who booked a seat for a train departing after 10:00 AM.
4.List all trains along with the count of bookings made for each. Include trains with zero
bookings.
8.Retrieve the train name, departure, and arrival stations for bookings made by a specific
passenger (e.g., 'John Doe').
9.List the passengers who booked a seat on a train but did not provide their names (NULL
values).
10.Find the trains with bookings made on a specific date (e.g., '2023-01-02').