0% found this document useful (0 votes)
4 views11 pages

Basic Select, Where, Distinct

The document provides a comprehensive overview of SQL queries and concepts, including basic SELECT statements, aggregate functions, JOINs, UNION operations, subqueries, and window functions. It includes examples for various scenarios such as fetching employee data, calculating salaries, and managing records. Additionally, it explains SQL normalization, constraints, and methods for data manipulation like UPDATE and DELETE.

Uploaded by

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

Basic Select, Where, Distinct

The document provides a comprehensive overview of SQL queries and concepts, including basic SELECT statements, aggregate functions, JOINs, UNION operations, subqueries, and window functions. It includes examples for various scenarios such as fetching employee data, calculating salaries, and managing records. Additionally, it explains SQL normalization, constraints, and methods for data manipulation like UPDATE and DELETE.

Uploaded by

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

‭🔹 1.

Basic SELECT, WHERE, DISTINCT‬


‭Q:‬‭Fetch employee names without duplicates who joined‬‭after 2020.‬

‭ ELECT DISTINCT name‬


S
‭FROM employees‬
‭WHERE join_date > '2020-12-31';‬

‭🔹 2. Aggregate Functions with GROUP BY‬


‭Q:‬‭Find total salary spent per department.‬

‭ ELECT department, SUM(salary) AS total_salary‬


S
‭FROM employees‬
‭GROUP BY department;‬

‭🔹 3. HAVING Clause‬
‭Q:‬‭Departments paying over ₹1,000,000 combined salary.‬

‭ ELECT department, SUM(salary) AS total_salary‬


S
‭FROM employees‬
‭GROUP BY department‬
‭HAVING SUM(salary) > 1000000;‬

‭🔹 4. ORDER BY & LIMIT / TOP‬


‭Q:‬‭Top 3 highest earning employees.‬

‭ ELECT name, salary‬


S
‭FROM employees‬
‭ORDER BY salary DESC‬
‭LIMIT 3;‬
TOP 3‬‭instead of‬‭
‭(Use‬‭ LIMIT 3‬‭in SQL Server)‬

‭🔹 5. JOINs‬
projects.manager_id‬‭ref.‬
‭Q:‬‭List projects with their manager names (assuming‬‭
employees.id‬
‭ ‭).‬

‭ ELECT p.project_name, e.name AS manager_name‬


S
‭FROM projects p‬
‭JOIN employees e‬
‭ON p.manager_id = e.id;‬

‭Explain the difference:‬

‭●‬ ‭INNER JOIN‬‭– only matching rows‬

‭●‬ ‭LEFT/RIGHT JOIN‬‭– includes unmatched rows from one‬‭table‬

‭●‬ F
‭ ULL OUTER JOIN‬‭– includes all rows from both tables‬‭(‬‭synergisticit.com‬‭,‬
‭synergisticit.com‬‭)‬

‭🔹 6. UNION vs UNION ALL‬


‭●‬ ‭UNION‬‭– removes duplicates‬

‭●‬ ‭UNION ALL‬‭– keeps duplicates‬

‭ ELECT name FROM employees‬


S
‭UNION‬
‭SELECT name FROM contractors;‬

‭🔹 7. Subqueries‬
‭Q:‬‭Employees earning above average salary.‬

‭ ELECT name, salary‬


S
‭FROM employees‬
‭WHERE salary > (‬
‭SELECT AVG(salary) FROM employees‬
‭);‬

‭🔹 8. Nested JOIN + WHERE vs JOIN filter‬


‭Q:‬‭Filter during JOIN vs after.‬

‭●‬ ‭Filtering in JOIN condition reduces intermediate result size;‬

‭●‬ I‭t can affect execution and results especially if it filters out rows before aggregations or‬
‭outer joins (‬‭reddit.com‬‭)‬

‭🔹 9. Find 2nd Highest Salary‬


‭ ELECT MAX(salary) AS second_max‬
S
‭FROM employees‬
‭WHERE salary < (‬
‭SELECT MAX(salary) FROM employees‬
‭);‬

‭Another option using window functions:‬

‭ ELECT DISTINCT salary‬


S
‭FROM (‬
‭SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk‬
‭FROM employees‬
‭) sub‬
‭WHERE rnk = 2;‬

‭🔹 10. Window Functions‬


‭Q:‬‭Show running total of salary per department.‬

‭SELECT id, name, department, salary,‬


‭SUM(salary) OVER (PARTITION BY department ORDER BY id‬
‭ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS‬
‭running_total‬
‭FROM employees;‬

‭ indow functions (a.k.a. analytics functions) like ROW_NUMBER(), AVG() OVER(...) etc., are‬
W
‭common in interviews (‬‭arxiv.org‬‭).‬

‭🔹 11. UPDATE with JOIN‬


‭Q:‬‭Give a ₹5,000 bonus to employees in Sales.‬

‭ PDATE employees e‬
U
‭SET bonus = bonus + 5000‬
‭WHERE e.department = 'Sales';‬

‭🔹 12. DELETE with Condition‬


‭Q:‬‭Remove temp employees.‬

‭ ELETE FROM employees‬


D
‭WHERE status = 'Temp';‬

‭🔹 13. CREATE Table + Constraints‬


‭Q:‬‭Create table with PK, NOT NULL, UNIQUE.‬

‭CREATE TABLE employees (‬


‭id INT PRIMARY KEY,‬
‭name VARCHAR(100) NOT NULL,‬
‭email VARCHAR(100) UNIQUE,‬
‭department VARCHAR(50)‬
‭);‬
‭🔹 14. Explain Normalization & Joins‬
‭●‬ ‭Normalization‬‭reduces redundancy: 1NF, 2NF, 3NF etc.‬‭(‬‭synergisticit.com‬‭)‬

‭●‬ E
‭ xplain‬‭SQL relationships‬‭: One-to-one, One-to-many,‬‭Many-to-many‬
‭(‬‭synergisticit.com‬‭)‬

‭●‬ ‭Types of joins: INNER, LEFT, RIGHT, FULL (‬‭synergisticit.com‬‭,‬‭synergisticit.com‬‭)‬

‭15. SQL Query to fetch employee(s) with max salary‬

SELECT * FROM employees‬


WHERE salary = (SELECT MAX(salary) FROM employees);‬


‭16.Find duplicates in an array‬

Set<Integer> set = new HashSet<>();‬


for (int num : arr) {‬


if (!set.add(num)) {‬

System.out.println("Duplicate: " + num);‬


}‬

}‬

🔹 1. Find employees whose name starts with‬

'A'‬

SELECT * FROM employees‬

WHERE name LIKE 'A%';‬


🔹 2. Get the number of employees in each‬



department‬

SELECT department, COUNT(*) AS employee_count‬

FROM employees‬

GROUP BY department;‬

🔹 3. Get employee details who have not been‬



assigned a project‬

SELECT * FROM employees‬

WHERE project_id IS NULL;‬


🔹 4. List all employees who joined in 2023‬



SELECT * FROM employees‬

WHERE YEAR(join_date) = 2023;‬

🔹 5. Get the average salary of employees in‬



the 'HR' department‬

SELECT AVG(salary) AS avg_salary‬

FROM employees‬

WHERE department = 'HR';‬


🔹 6. Find the third highest salary‬



SELECT DISTINCT salary‬

FROM employees‬

ORDER BY salary DESC‬


LIMIT 1 OFFSET 2;‬


Or using subquery:‬

SELECT MIN(salary) AS third_highest‬


FROM (‬

SELECT DISTINCT salary‬


FROM employees‬

ORDER BY salary DESC‬



LIMIT 3‬

) AS temp;‬

🔹 7. Find employees who earn more than their‬



department average‬

SELECT name, salary, department‬

FROM employees e‬

WHERE salary > (‬


SELECT AVG(salary)‬

FROM employees‬

WHERE department = e.department‬


);‬

🔹 8. Display department name and number of‬



employees using JOIN‬

SELECT d.dept_name, COUNT(e.id) AS employee_count‬

FROM departments d‬

LEFT JOIN employees e ON d.id = e.dept_id‬


GROUP BY d.dept_name;‬

🔹 9. Get employees who work on more than one‬

project‬

Assuming a‬‭
‭ employee_project‬‭
table with many-to-many‬‭
relationship:‬

SELECT employee_id, COUNT(project_id) AS project_count‬


FROM employee_project‬

GROUP BY employee_id‬

HAVING COUNT(project_id) > 1;‬


🔹 10. List duplicate employee names (same‬



name more than once)‬

SELECT name, COUNT(*) AS count‬

FROM employees‬

GROUP BY name‬

HAVING COUNT(*) > 1;‬


🔹 11. Update salary: give 10% raise to‬



‘Sales’ department‬

UPDATE employees‬

SET salary = salary * 1.10‬

WHERE department = 'Sales';‬


🔹 12. Delete all interns‬



DELETE FROM employees‬

WHERE job_title = 'Intern';‬


🔹 13. Select all employees who joined‬



between Jan and March‬

SELECT * FROM employees‬

WHERE MONTH(join_date) BETWEEN 1 AND 3;‬


🔹 14. Find total salary paid in each month‬



SELECT MONTH(payment_date) AS month, SUM(amount) AS total_paid‬

FROM salaries‬

GROUP BY MONTH(payment_date);‬

🔹 15. Display employee names and their‬

manager’s name‬

Assuming‬‭
‭ manager_id‬‭
is a foreign key in the same‬‭
employees‬‭
table:‬

SELECT e.name AS employee_name, m.name AS manager_name‬


FROM employees e‬

LEFT JOIN employees m ON e.manager_id = m.id;‬


You might also like