Write a SQL query to list employees who have a salary above the average salary of all…
- User
Write a SQL query to list employees who have a salary above the average salary of all employees.
drop table if exists employees; CREATE TABLE employees ( employee_id INT PRIMARY KEY, employee_name VARCHAR(100), salary DECIMAL(10, 2), department_id INT );
INSERT INTO employees (employee_id, employee_name, salary, department_id) VALUES (1, 'Ravi', 60000, 1), (2, 'Aisha', 50000, 1), (3, 'Rajesh', 80000, 2), (4, 'Sita', 70000, 2), (5, 'Vikram', 55000, 1);
- Assistant
To list the employees who have a salary above the average salary of all employees, you can use the following SQL query:
SELECT employee_id, employee_name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);This query works as follows:
- The subquery
(SELECT AVG(salary) FROM employees)calculates the average salary of all employees in theemployeestable. - The main query selects the
employee_id,employee_name, andsalaryof employees whose salary is greater than this average value obtained from the subquery.
- The subquery
- User
List products with stock less than 10.
📚 Dataset: CREATE TABLE products ( product_id INT PRIMARY KEY, product_name VARCHAR(100), stock INT ); INSERT INTO products VALUES (1, 'Smartphone', 5), (2, 'Laptop', 20), (3, 'T-shirt', 8);
- Assistant
To list the products with stock less than 10, you can execute the following SQL query:
SELECT product_id, product_name, stock FROM products WHERE stock < 10;This query retrieves the
product_id,product_name, andstockfor each product in theproductstable where thestockis less than 10.