SQL Flashcards

1
Q

Given the employees table (department_id), write a query to select all columns from the employees table where the department_id is 5.

A
SELECT *
FROM employees
WHERE department_id = 5;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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’.

A
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE 'S%';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Given the orders table, write a query to find the total number of orders.

A
SELECT COUNT(*) AS total_orders
FROM orders;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Using the employees table (salary, department_id), write a query to calculate the average salary of employees in each department_id.

A
SELECT department_id, AVG(salary) AS average_salary
FROM employees
GROUP BY department_id;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Given the employees table (salary), write a query to list the top 3 highest-paid employees.

A
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 3;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A
SELECT e.first_name, e.last_name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Given the employees table (manager_id), write a query to find employees who do not have a manager.

A
SELECT *
FROM employees
WHERE manager_id IS NULL;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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%.

A
UPDATE employees
SET salary = salary * 1.10
WHERE department_id = 3;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Given the temp_data table (created_at), write a query to delete all records where created_at is older than ‘2023-01-01’.

A
DELETE FROM temp_data
WHERE created_at < '2023-01-01';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Using the users table (email), write a query to create an index on the email column.

A
CREATE INDEX idx_users_email
ON users(email);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Given the employees table (job_title), write a query to select the distinct job_title values.

A
SELECT DISTINCT job_title
FROM employees;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Using the employees table (salary), write a query to find the maximum and minimum salary.

A
SELECT MAX(salary) AS max_salary, MIN(salary) AS min_salary
FROM employees;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.

A
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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.

A
SELECT first_name, last_name
FROM employees
WHERE department_id = (
    SELECT department_id
    FROM departments
    WHERE department_name = 'Sales'
);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Given the employees table, write a query to add a new column birthdate of type DATE.

A
ALTER TABLE employees
ADD birthdate DATE;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Given the employees table (salary), write a query to retrieve the second highest salary.

A
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
17
Q

Using the employees table (department_id), write a query to find all department_id that have more than 10 employees.

A
SELECT department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 10;
18
Q

Given the employees table (employee_id, manager_id, first_name, last_name, salary), write a query to list employees who have the same salary as their manager.

A
SELECT e.first_name, e.last_name
FROM employees e
JOIN employees m ON e.manager_id = m.employee_id
WHERE e.salary = m.salary;
19
Q

Using the employees table (department_id, employee_id, salary), write a query to calculate the cumulative salary for each department_id ordered by employee_id.

A
SELECT department_id, SUM(salary) OVER (PARTITION BY department_id ORDER BY employee_id) AS cumulative_salary
FROM employees
ORDER BY department_id;
20
Q

Given the employees table (employee_id, first_name, last_name) and sales table (employee_id, amount), write a query to create a view named v_employee_sales that shows first_name, last_name, and the total_sales amount for each employee.

A
CREATE VIEW v_employee_sales AS
SELECT e.first_name, e.last_name, SUM(s.amount) AS total_sales
FROM employees e
JOIN sales s ON e.employee_id = s.employee_id
GROUP BY e.first_name, e.last_name;