give me most ask sql queries with answers in interview
- User
give me most ask sql queries with answers in interview
- Assistant
Certainly! SQL is a critical skill for many technical roles, and interviewers often test candidates on various query-writing tasks and SQL concepts. Below are some commonly asked SQL-related interview questions along with their answers:
1. Retrieve all records from a table
Question: Write a SQL query to select all records from a table named
employees.Answer:
SELECT * FROM employees;2. Retrieve distinct values
Question: How would you extract distinct job titles from an
employeestable?Answer:
SELECT DISTINCT job_title FROM employees;3. Filter records using a
WHEREclauseQuestion: Write a query to find employees whose salary is greater than 50,000.
Answer:
SELECT * FROM employees WHERE salary > 50000;4. Using
ORDER BYQuestion: How do you sort employees by their last names in ascending order?
Answer:
SELECT * FROM employees ORDER BY last_name ASC;5. Count the number of records
Question: How do you count the number of employees in the
employeestable?Answer:
SELECT COUNT(*) FROM employees;6. Aggregate functions (
SUM,AVG, etc.)Question: Calculate the average salary of employees.
Answer:
SELECT AVG(salary) FROM employees;7. Group records using
GROUP BYQuestion: How would you get the total number of employees in each department?
Answer:
SELECT department_id, COUNT(*) FROM employees GROUP BY department_id;8. Using
HAVINGwithGROUP BYQuestion: Find departments with more than 10 employees.
Answer:
SELECT department_id, COUNT(*) AS num_employees FROM employees GROUP BY department_id HAVING COUNT(*) > 10;9. Inner Join
Question: Write a query to join the
employeesanddepartmentstables to list employees’ names along with their department names.Answer:
SELECT e.first_name, e.last_name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id;10. Left Join
Question: How do you retrieve all employees, including those who do not have a department?
Answer:
SELECT e.first_name, e.last_name, d.department_name FROM employees e LEFT JOIN departments d ON e.department_id = d.department_id;11. Find records using
LIKEQuestion: How would you find employees whose names start with 'A'?
Answer:
SELECT * FROM employees WHERE first_name LIKE 'A%';12. Using
INoperatorQuestion: Select employees who work in department 10, 20, or 30.
Answer:
SELECT * FROM employees WHERE department_id IN (10, 20, 30);13. Subquery
Question: Write a SQL query to find employees who earn more than the average salary.
Answer:
SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);14. Update records
Question: How do you increase the salary of all employees in department 5 by 10%?
Answer:
UPDATE employees SET salary = salary * 1.10 WHERE department_id = 5;15. Delete records
Question: Write a query to delete all employees in department 30.
Answer:
DELETE FROM employees WHERE department_id = 30;These queries cover a wide range of SQL functionalities, such as data retrieval, filtering, aggregations, joins, and data manipulation, which can often be expected in an SQL interview context.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.