Show List
SQL Coding Questions
- Write a SQL query to find the total number of employees in the employee table.
sqlCopy code
SELECT COUNT(*) FROM employee;
- Write a SQL query to display the names of all employees whose salary is greater than 50000.
sqlCopy code
SELECT name FROM employee WHERE salary > 50000;
- Write a SQL query to display the names and salaries of all employees who work in the IT department.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT';
- Write a SQL query to display the names and salaries of the top 5 highest-paid employees.
sqlCopy code
SELECT name, salary FROM employee ORDER BY salary DESC LIMIT 5;
- Write a SQL query to display the names of all employees who have the letter 'a' in their name.
sqlCopy code
SELECT name FROM employee WHERE name LIKE '%a%';
- Write a SQL query to display the average salary of all employees.
sqlCopy code
SELECT AVG(salary) FROM employee;
- Write a SQL query to display the names and salaries of all employees who work in the IT department and have a salary greater than 60000.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND salary > 60000;
- Write a SQL query to display the names and salaries of all employees who have the letter 'a' in their name and work in the IT department.
sqlCopy code
SELECT name, salary FROM employee WHERE name LIKE '%a%' AND department = 'IT';
- Write a SQL query to display the names and salaries of all employees who were hired in the year 2022.
sqlCopy code
SELECT name, salary FROM employee WHERE YEAR(hire_date) = 2022;
- Write a SQL query to display the names and salaries of all employees who work in the IT department or the HR department.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' OR department = 'HR';
- Write a SQL query to display the names and salaries of all employees who work in the IT department and were hired in the year 2022.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND YEAR(hire_date) = 2022;
- Write a SQL query to display the names and salaries of all employees who have the letter 'a' in their name and were hired in the year 2022.
sqlCopy code
SELECT name, salary FROM employee WHERE name LIKE '%a%' AND YEAR(hire_date) = 2022;
- Write a SQL query to display the names and salaries of all employees who have a salary between 40000 and 60000.
sqlCopy code
SELECT name, salary FROM employee WHERE salary BETWEEN 40000 AND 60000;
- Write a SQL query to display the names and salaries of all employees who have the letter 'a' as the second letter in their name.
sqlCopy code
SELECT name, salary FROM employee WHERE name LIKE '_a%';
- Write a SQL query to display the names and salaries of all employees who work in the IT department and have a salary between 50000 and 70000.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND salary BETWEEN 50000 AND 70000;
- Write a SQL query to display the names and salaries of all employees who were hired in the month of January.
sqlCopy code
SELECT name, salary FROM employee WHERE MONTH(hire_date) = 1;
- Write a SQL query to display the names and salaries of all employees who were hired in the year 2021 and work in the IT department.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND YEAR(hire_date) = 2021;
- Write a SQL query to display the names and salaries of all employees who work in the IT department and were hired in the month of March.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND MONTH(hire_date) = 3;
- Write a SQL query to display the names and salaries of all employees who have a salary greater than the average salary.
sqlCopy code
SELECT name, salary FROM employee WHERE salary > (SELECT AVG(salary) FROM employee);
- Write a SQL query to display the names and salaries of all employees who have the letter 'a' in their name and were hired in the year 2021.
sqlCopy code
SELECT name, salary FROM employee WHERE name LIKE '%a%' AND YEAR(hire_date) = 2021;
- Write a SQL query to display the names and salaries of all employees who work in the IT department and have a salary greater than the average salary of the IT department.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND salary > (SELECT AVG(salary) FROM employee WHERE department = 'IT');
- Write a SQL query to display the names and salaries of all employees who were hired in the year 2021 or later.
sqlCopy code
SELECT name, salary FROM employee WHERE hire_date >= '2021-01-01';
- Write a SQL query to display the names and salaries of all employees who work in the IT department and were hired in the year 2021 or later.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND hire_date >= '2021-01-01';
- Write a SQL query to display the names and salaries of all employees who work in the IT department and have a salary in the top 10% of salaries in the IT department.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND salary >= (SELECT PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY salary) FROM employee WHERE department = 'IT');
- Write a SQL query to display the names and salaries of all employees who have the highest salary in their department.
sqlCopy code
SELECT name, salary FROM employee e1 WHERE salary = (SELECT MAX(salary) FROM employee e2 WHERE e1.department = e2.department);
- Write a SQL query to display the names and salaries of all employees who work in the IT department and were hired in the year 2021 or later, sorted by salary in descending order.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND hire_date >= '2021-01-01' ORDER BY salary DESC;
- Write a SQL query to display the names and salaries of all employees who work in the IT department and have a salary that is within $1000 of the highest salary in the IT department.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND ABS(salary - (SELECT MAX(salary) FROM employee WHERE department = 'IT')) <= 1000;
- Write a SQL query to display the names and salaries of the top 3 highest-paid employees in each department.
sqlCopy code
SELECT name, salary, department FROM (
SELECT name, salary, department, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC)
- Write a SQL query to display the names and salaries of all employees who work in the IT department and have a salary greater than $80,000.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND salary > 80000;
- Write a SQL query to display the names and hire dates of all employees who were hired in the year 2021 and have been employed for less than six months.
sqlCopy code
SELECT name, hire_date FROM employee WHERE hire_date >= '2021-01-01' AND DATEDIFF(MONTH, hire_date, GETDATE()) < 6;
- Write a SQL query to display the names, salaries, and hire dates of all employees who were hired in the year 2021 and have a salary greater than or equal to $60,000.
sqlCopy code
SELECT name, salary, hire_date FROM employee WHERE salary >= 60000 AND YEAR(hire_date) = 2021;
- Write a SQL query to display the names and salaries of all employees who work in the IT department or the HR department.
sqlCopy code
SELECT name, salary FROM employee WHERE department IN ('IT', 'HR');
- Write a SQL query to display the names and salaries of all employees who work in the IT department and have a salary greater than the average salary of the IT department.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND salary > (SELECT AVG(salary) FROM employee WHERE department = 'IT');
- Write a SQL query to display the names and salaries of all employees who work in the IT department and have a salary in the range of $70,000 to $90,000.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND salary BETWEEN 70000 AND 90000;
- Write a SQL query to display the names and salaries of all employees who work in the IT department and were hired in the year 2021 or later, sorted by hire date in ascending order.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND hire_date >= '2021-01-01' ORDER BY hire_date ASC;
- Write a SQL query to display the names and salaries of all employees who have a salary greater than the average salary of their department.
sqlCopy code
SELECT name, salary FROM employee WHERE salary > (SELECT AVG(salary) FROM employee WHERE department = e.department);
- Write a SQL query to display the names and salaries of all employees who have the highest salary in their department and work in the IT department.
sqlCopy code
SELECT name, salary FROM employee e1 WHERE salary = (SELECT MAX(salary) FROM employee e2 WHERE e1.department = e2.department) AND department = 'IT';
- Write a SQL query to display the names and salaries of all employees who work in the IT department and were hired in the year 2021 or later, sorted by hire date in descending order.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND hire_date >= '2021-01-01' ORDER BY hire_date DESC;
- Write a SQL query to display the names and salaries of all employees who work in the IT department and have a salary that is within $5,000 of the highest salary in the IT department.
sqlCopy code
SELECT name, salary FROM employee WHERE department = 'IT' AND salary >= (SELECT MAX(salary) - 5000 FROM employee WHERE department = 'IT');
Leave a Comment