SQL_Interview_Questions
SQL_Interview_Questions
Q: How can you delete duplicates while keeping just one entry?
A: DELETE FROM table_name WHERE id NOT IN (SELECT MIN(id) FROM table_name GROUP
BY column1, column2);
Q: How do you find records in one table that dont exist in another?
A: SELECT * FROM table1 WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE table1.id =
table2.id);
Q: What's the key difference between INNER JOIN and LEFT JOIN?
A: INNER JOIN returns only matching records, while LEFT JOIN returns all records from the left
table and matching records from the right table.
Q: How do different types of joins impact the number of records in the output?
A: INNER JOIN: Only common records.
LEFT JOIN: All left records, NULLs for missing right.
RIGHT JOIN: All right records, NULLs for missing left.
FULL OUTER JOIN: All records from both tables.
Q: How do you find the earliest and latest records for each group in a table?
A: SELECT department, MIN(join_date) AS earliest, MAX(join_date) AS latest FROM employees
GROUP BY department;