IntermediateINSERT, UPDATE, DELETE
Bulk INSERT with CTE and SELECT
The query
SQL
WITH new_employees (first_name, last_name, email, dept_name, salary) AS (
VALUES
('Amit', 'Kumar', 'amit@co.com', 'Engineering', 65000),
('Neha', 'Gupta', 'neha@co.com', 'Marketing', 55000),
('Raj', 'Patel', 'raj@co.com', 'Finance', 70000)
)
INSERT INTO employees (first_name, last_name, email, dept_id, salary, hire_date, status)
SELECT
ne.first_name,
ne.last_name,
ne.email,
d.dept_id,
ne.salary,
CURRENT_DATE,
'active'
FROM new_employees ne
JOIN departments d ON d.dept_name = ne.dept_name
RETURNING emp_id, first_name, last_name;Note
“CTE as data source for INSERT. Joins departments to resolve dept_name to dept_id automatically.