Complete the code to select the employee name and their department name using a scalar subquery.
SELECT employee_name, (SELECT [1] FROM departments WHERE departments.id = employees.department_id) AS department_name FROM employees;The scalar subquery must select the name of the department to show it alongside the employee name.
Complete the code to find the total number of orders for each customer using a scalar subquery.
SELECT customer_name, (SELECT [1] FROM orders WHERE orders.customer_id = customers.id) AS total_orders FROM customers;The scalar subquery must count the number of orders, so COUNT(*) is used.
Fix the error in the scalar subquery to get the highest salary from the employees table.
SELECT employee_name, (SELECT MAX([1]) FROM employees) AS highest_salary FROM employees;The MAX function should be applied to the salary column to find the highest salary.
Fill both blanks to select each product's name and its category name using a scalar subquery.
SELECT product_name, (SELECT [1] FROM categories WHERE categories.id = products.[2]) AS category_name FROM products;
The subquery selects the name from categories where the category's id matches the product's category_id.
Fill all three blanks to select each student's name, their highest test score, and the test date using scalar subqueries.
SELECT student_name, (SELECT MAX([1]) FROM test_scores WHERE test_scores.student_id = students.[2]) AS highest_score, (SELECT [3] FROM test_scores WHERE test_scores.student_id = students.id ORDER BY score DESC LIMIT 1) AS test_date FROM students;
The first subquery finds the maximum score for each student matching by id. The second subquery selects the test_date of the highest score.