0% found this document useful (0 votes)
3 views

SQL

The document contains a series of SQL queries addressing various tasks related to employee and contractor data, including retrieving employee details, calculating total salaries, and finding average salaries. It also includes queries for aggregating sales data and joining tables to summarize customer spending. Additionally, it demonstrates the use of conditional statements and grouping in SQL queries.

Uploaded by

Aaquib Sattar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL

The document contains a series of SQL queries addressing various tasks related to employee and contractor data, including retrieving employee details, calculating total salaries, and finding average salaries. It also includes queries for aggregating sales data and joining tables to summarize customer spending. Additionally, it demonstrates the use of conditional statements and grouping in SQL queries.

Uploaded by

Aaquib Sattar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

 SELECT employee_id, first_name, last_name FROM employees WHERE date_join > '2020-01-01';

Question: Write a SQL query to get the department, COUNT(*) (number of employees), and the
AVG(salary) for each department from the employees table. The results should be grouped by
department.

 SELECT first_name, last_name, salary FROM employees WHERE salary IN


(SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 5);

Question: Write a SQL query to get the department and the total salary (SUM(salary)) for each
department in the employees table. The result should be ordered by the total salary in descending
order.

 SELECT department, SUM (salary) TotalSalary FROM employees GROUP BY department ORDER
BY TotalSalary DESC;

Question: Write a SQL query to find the second highest salary from the employees table.

 SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);

Question: Write a SQL query to find all employees who have the same salary as the average salary of
all employees in the company.

 SELECT first_name, last_name FROM employees WHERE salary = (SELECT AVG(salary) FROM
employees);

Question: Write a SQL query to find the total number of employees in each department, and display
the department name along with the number of employees.

 SELECT department, COUNT(*) Total_Num_Emp FROM employees GROUP BY department;

Question: Write an SQL query to find the total sales value for each product (i.e., quantity *
sale_price), grouped by product_id.

 SELECT product_id, SUM(quantity*sale_price) sales_Value FROM Sales GROUP BY product_id;


Question 15: (UNION)

Employees

emp_id first_name last_name department

1 John Doe HR

2 Jane Smith IT

3 Alice Johnson IT

Contractors

contractor_id first_name last_name department

101 Bob Brown HR

102 Sarah Lee IT

Question: Write an SQL query to get a list of all employees and contractors (both first and last
names), from both the Employees and Contractors tables.

 SELECT first_name, last_name FROM Employees UNION SELECT first_name, last_name FROM
Contractors

Question 17: (CASE Statement)

You have the following table:

Employees

emp_id first_name last_name department salary

1 John Doe HR 50000

2 Jane Smith IT 60000

3 Alice Johnson IT 75000

4 Bob Brown HR 55000

5 Charlie Lee IT 65000

Question: Write an SQL query to create a column salary_grade where:

 "Low" for salary less than 55000

 "Medium" for salary between 55000 and 70000

 "High" for salary greater than 70000

 SELECT emp_id, first_name, last_name, salary,


CASE

WHEN salary < 55000 THEN 'Low'

WHEN salary BETWEEN 55000 AND 70000 THEN 'Medium'

ELSE 'High'

END AS salary_grade

FROM Employees;

Great! Here's your next SQL query quiz:

Question 19: (JOIN with Aggregation) You have the following two tables:

Orders

order_id customer_id order_date total_amount


101 1 2025-03-01 250
102 2 2025-03-03 300
103 1 2025-03-05 450
104 3 2025-03-07 500
105 2 2025-03-10 200

Customers

customer_id first_name last_name


1 John Doe
2 Jane Smith
3 Alice Johnson

Question: Write an SQL query to find the total amount spent by each customer (sum of
total_amount), along with their first_name and last_name, ordered by the total amount in
descending order.

 SELECT c.first_name, c.last_name, SUM(o.total_amount) AS total_Order FROM


Customers c JOIN Orders o USING (customer_id) GROUP BY c.customer_id, c.first_name,
c.last_name ORDER BY total_Order DESC;

You should also include c.first_name and c.last_name in the GROUP BY clause to avoid SQL
errors. This is because any column in the SELECT list that is not aggregated (like
SUM(o.total_amount)) needs to be part of the GROUP BY clause.

You might also like