Hard sql

MySQL subquery above avg #22

Problem

MySQL: MySQL subquery above avg #22

Hints
  • Use MySQL docs syntax for LIMIT and functions

Your practice code

Ready — edit the code above and click Run.

Solution

-- MySQL sample schema
CREATE TABLE employees (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  dept_id INT,
  salary DECIMAL(10,2),
  hired DATE
);
CREATE TABLE departments (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO employees (name, dept_id, salary, hired) VALUES
  ('Ali', 1, 90000.00, '2020-01-15'),
  ('Sara', 2, 120000.00, '2019-06-01'),
  ('Raj', 1, 75000.00, '2021-03-20');
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales');
SELECT name, salary FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

Try solving on your own first, then reveal the official answer.

Explanation

MySQL-specific syntax: LIMIT, AUTO_INCREMENT, ENGINE=InnoDB, DELIMITER for procedures.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details