SQL Flashcards
Given the employees table (department_id), write a query to select all columns from the employees table where the department_id is 5.
SELECT * FROM employees WHERE department_id = 5;
Using the employees table (first_name, last_name), write a query to retrieve the first_name and last_name of employees whose last_name starts with ‘S’.
SELECT first_name, last_name FROM employees WHERE last_name LIKE 'S%';
Given the orders table, write a query to find the total number of orders.
SELECT COUNT(*) AS total_orders FROM orders;
Using the employees table (salary, department_id), write a query to calculate the average salary of employees in each department_id.
SELECT department_id, AVG(salary) AS average_salary FROM employees GROUP BY department_id;
Given the employees table (salary), write a query to list the top 3 highest-paid employees.
SELECT * FROM employees ORDER BY salary DESC LIMIT 3;
Using the employees table (first_name, last_name) and departments table (department_name), write a query to join these tables on department_id and retrieve first_name, last_name, and department_name.
SELECT e.first_name, e.last_name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id;
Given the employees table (manager_id), write a query to find employees who do not have a manager.
SELECT * FROM employees WHERE manager_id IS NULL;
Using the employees table (department_id, salary), write a query to update the salary of employees in department_id 3 by increasing it by 10%.
UPDATE employees SET salary = salary * 1.10 WHERE department_id = 3;
Given the temp_data table (created_at), write a query to delete all records where created_at is older than ‘2023-01-01’.
DELETE FROM temp_data WHERE created_at < '2023-01-01';
Using the users table (email), write a query to create an index on the email column.
CREATE INDEX idx_users_email ON users(email);
Given the employees table (job_title), write a query to select the distinct job_title values.
SELECT DISTINCT job_title FROM employees;
Using the employees table (salary), write a query to find the maximum and minimum salary.
SELECT MAX(salary) AS max_salary, MIN(salary) AS min_salary FROM employees;
Given the employees table (first_name, last_name, manager_id), write a query to list all employees and their corresponding manager’s first_name and last_name.
SELECT e.first_name AS employee_first, e.last_name AS employee_last, m.first_name AS manager_first, m.last_name AS manager_last FROM employees e LEFT JOIN employees m ON e.manager_id = m.employee_id;
Using the employees table (first_name, last_name) and departments table (department_id, department_name), write a query to retrieve the first_name and last_name of employees who work in the ‘Sales’ department using a subquery.
SELECT first_name, last_name FROM employees WHERE department_id = ( SELECT department_id FROM departments WHERE department_name = 'Sales' );
Given the employees table, write a query to add a new column birthdate of type DATE.
ALTER TABLE employees ADD birthdate DATE;