SQL DML Interview Questions
2 questions with answers · SQL Interview Guide
INSERT, UPDATE, DELETE, and MERGE operations. Data Manipulation Language for modifying data within tables.
Write SQL Query to find duplicate rows in a database? and then write SQL query to delete them
To find duplicate rows, you GROUP BY the columns that define a duplicate and look for groups where the count is greater than 1. To delete them, you typically keep one row per group, usually identified by the lowest or highest primary key, and delete the rest.
-- Find duplicates based on name and department_id
SELECT name, department_id, COUNT(*) AS cnt
FROM employees
GROUP BY name, department_id
HAVING COUNT(*) > 1;
-- Delete duplicates, keeping the row with the lowest employee_id
DELETE FROM employees
WHERE employee_id NOT IN (
SELECT MIN(employee_id)
FROM employees
GROUP BY name, department_id
);How to update a database table using SQL
You use the UPDATE statement to modify existing rows in a table. Always pair it with a WHERE clause to target specific rows, because without one you will update every row in the table, which is almost never what you want. You can update multiple columns in a single statement by separating them with commas in the SET clause.
-- Update a single row
UPDATE employees
SET salary = 75000, department_id = 3
WHERE employee_id = 101;
-- Update based on a condition across many rows
UPDATE employees
SET salary = salary * 1.10
WHERE department_id = 5;Knowing the answers is half the battle
The other half is explaining them clearly under pressure.
Try a free mock interviewarrow_forward