Filtering and sorting
1. Retrieve all customers with names starting with “A” and ending with “n”.
Dataset: Customers (customer_id, customer_name)
SELECT customer_id, customer_name
FROM Customers
WHERE customer_name LIKE 'A%n';
2. Find the products with names containing at least one digit.
Dataset: Products (product_id, product_name)
SELECT product_id, product_name
FROM Products
WHERE product_name LIKE '%[0-9]%';
___________________________________________________________
SELECT product_id, product_name
FROM Products
WHERE product_name LIKE '%0%'
OR product_name LIKE '%1%'
OR product_name LIKE '%2%'
OR product_name LIKE '%3%'
OR product_name LIKE '%4%'
OR product_name LIKE '%5%'
OR product_name LIKE '%6%'
OR product_name LIKE '%7%'
OR product_name LIKE '%8%'
OR product_name LIKE '%9%';
3. Get the list of employees sorted by their salary in ascending order. NULL values should appear at
the end.
Dataset: Employees (employee_id, employee_name, salary)
SELECT employee_id, employee_name, salary
FROM Employees
ORDER BY
CASE
WHEN salary IS NULL THEN 1
ELSE 0
END,
salary ASC;
Sinduja Anthannagari
4. Retrieve the customers whose names contain exactly five characters.
Dataset: Customers (customer_id, customer_name)
SELECT customer_id, customer_name
FROM Customers
WHERE LEN(customer_name) = 5;
__________________________________________________________________
SELECT customer_id, customer_name
FROM Customers
WHERE customer_name LIKE '_____';
5. Find the products with names starting with “S” and ending with “e”.
Dataset: Products (product_id, product_name)
SELECT *
FROM Products
WHERE product_name LIKE 'S%e'
6. Get the list of employees sorted by their last name and then by their first name.
Dataset: Employees (employee_id, first_name, last_name, salary)
SELECT *
FROM Employees
ORDER BY last_name, first_name;
7. Retrieve the orders placed on a specific date and sort them by the customer name in alphabetical
order.
Dataset: Orders (order_id, order_date, customer_id)
SELECT o.order_id, o.order_date, o.customer_id
FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id
WHERE o.order_date = '2024-06-15' -- Replace with your specific date
ORDER BY c.customer_name ASC;
8. Find the products with names containing exactly three letters.
Dataset: Products (product_id, product_name)
SELECT *
FROM Products
WHERE LEN(product_name) = 3;
Sinduja Anthannagari
9. Get the list of employees sorted by their salary in descending order. NULL values should appear at
the beginning.
Dataset: Employees (employee_id, employee_name, salary)
SELECT *
FROM Employees
ORDER BY
CASE WHEN salary IS NULL
THEN 0
ELSE 1
END, salary DESC;
10. Retrieve the customers whose names contain a space character.
Dataset: Customers (customer_id, customer_name)
SELECT *
FROM Customers
WHERE customer_name LIKE '% %';
11. Get the list of employees who are working as manager earning 2500 or more salary.
SELECT * FROM employees
WHERE job_title = 'Manager' AND salary >= 2500;
12. Get the list of employees who are working in 10th department and also Data Engineer and
earning less than or equal to 1300.
SELECT * FROM employees
WHERE department_id = 10 AND job_title = 'Data Engineer' AND salary <= 1300;
13. Get the list of employees who are not working as Data Engineer
SELECT * FROM employees
WHERE job_title <> 'Data Engineer';
14. Get the list of employees who are not working in 20th department.
SELECT * FROM employees
WHERE department_id <> 20;
_____________________________________________________
SELECT * FROM employees
WHERE department_id != 20;
15. Get the list of employees who are working as Data Engineer, Data Analyst.
SELECT * FROM employees
WHERE job_title IN ('Data Engineer', 'Data Analyst');
16. Get the list of employees who are working in 20th,30th departments.
Sinduja Anthannagari
SELECT * FROM employees
WHERE department_id IN (20, 30);
17. Get the list of employees who are except in 10th ,30th departments.
SELECT * FROM employees
WHERE department_id NOT IN (10, 30);
18. Get the list of employees who names are Anusha, Presto, Sindu.
SELECT * FROM employees
WHERE name IN ('Anusha', 'Presto', 'Sindu');
19. Get the list of employee’s name, job, salary and department of those employees who are
working as Manager, Data Engineer and also working in 40th department. (using Logical & Relational
operator)
SELECT name, job, salary, department_id FROM employees
WHERE job IN ('Manager', 'Data Engineer') AND department_id = 40;
20. Get the list of employees who are working in 40,60th departments.
SELECT * FROM employees
WHERE department_id IN (40, 60);
21. Get the list of employees who are not working as Data Analyst and Data Engineer
SELECT * FROM employees
WHERE job_title NOT IN ('Data Analyst', 'Data Engineer');
22. Get the list of employees who are earning 20000 to 40000.
SELECT * FROM employees
WHERE salary BETWEEN 20000 AND 40000;
23. Get the list of employees who are not earning between 16000 to 28000.
SELECT * FROM employees
WHERE salary NOT BETWEEN 16000 AND 28000;
24. Get the list of employees who joined on 15-06-2024.
SELECT * FROM employees
WHERE join_date = '2024-06-15';
25. Get the list of employees who are joined after 01-06-2024.
SELECT * FROM employees
WHERE join_date > '2024-06-01';
26. Get the list of employees who are joined between 01-01-2024 to 01-06-2024.
SELECT * FROM employees
WHERE join_date BETWEEN '2024-01-01' AND '2024-06-01';
Sinduja Anthannagari
27. Get the list of employees who are not working as Data Engineer.
SELECT * FROM employees
WHERE job_title <> 'Data Engineer';
_________________________________________________
SELECT * FROM employees
WHERE job_title != 'Data Engineer';
28. Get the list of employees who are having A as First Character.
SELECT * FROM employees
WHERE name LIKE 'A%';
29. Get the list of employees who are having S in their names.
SELECT * FROM employees
WHERE name LIKE '%S%';
30. Get the list of employees who are having character A as 2nd in their names.
SELECT * FROM employees
WHERE name LIKE '_A%';
31. Get the list of employees who are getting bonus.
SELECT * FROM employees
WHERE bonus IS NOT NULL;
32. Get the list of employees who are not getting bonus.
SELECT * FROM employees
WHERE bonus IS NULL;
33. Get the list of employees who is boss of all.
SELECT * FROM employees
WHERE Manager IS NULL;
Sinduja Anthannagari