14th Mar 2017 Updated: 5th Nov 2024 8 minutes read Common SQL Job Interview Questions Marek Pankowski sql interview questions Table of Contents Theoretical SQL Questions for Job Interviews Practical SQL Exercises for Interview 1: Students in the Same City as Their School 2: Courses and Their Maximum Scores 3: Find the Error in the Query 4: Find the Result of the Query 5: Find the Highest Salary in Each Department 6: Find Current Date 7: Age of customers 8: Find Duplicate Rows 9: Courses with Three or More Students 10: Average Age of Employees in Each Company Ace Your Interview with the SQL Practice Track Congratulations! Your SQL skills were strong enough to get you that job interview! Now, if you only knew what SQL questions and practical exercises a recruiter might ask you to do… This article is meant as a SQL practice for interviews. I’ll help you prepare for the SQL and database aspects of your job interview. Knowing SQL opens up opportunities in and out of IT, as this query language is used practically everywhere. Let’s say you finally made your decision to use your SQL skills in a new job. Before you can start, though, you need to get through the job interview. Your interviewer may use several kinds of SQL whiteboard questions: sometimes you’ll be asked to describe the differences between two elements; sometimes you’ll have to tell them the result of a specific query; you may even have to write some simple code. This sounds stressful, but don’t worry too much. The best way to prepare for an interview is tons of practice. I recommend the SQL Practice track at LearnSQL.com. It contains over 800 interactive SQL exercises to help you review and practice SQL before an interview. In this article, I’ll help you prepare for the by going over some common questions and SQL exercises that recruiters use during the interview process. Theoretical SQL Questions for Job Interviews First, let’s focus on the more theoretical side of SQL. To assess your understanding here, the recruiter will likely ask you to verbally explain some simpler questions. These are usually mainly about the elementary parts of SQL. For example, you might be asked to answer some of the following SQL whiteboard questions: Enumerate and explain all the basic elements of an SQL query. What is the WHERE clause? What do the LIKE and NOT LIKE operators do? Explain the usage of AND, OR, and NOT clauses. What is a NULL value? What does a JOIN do? What are the different types of JOIN clauses? Explain the GROUP BY clause. What is the difference between the WHERE and HAVING clauses? What does ORDER BY do? What does UNION do? What is the difference between UNION and UNION ALL? What do the INTERSECT and MINUS clauses do? What is the role of the DISTINCT keyword? Explain the use of aliases in queries. Can you answer every question from my SQL practice for interviews? Click on each question to go to the article that discuss each topic in detail. Remember that even basic questions need good preparation and a lot of practice. Our online courses can help you brush up on any semi-forgotten skills. Practical SQL Exercises for Interview Technical job interviews often have a practical side. This is where you need to prove your SQL skills as you would use them in real life – apart from answering typical SQL whiteboard questions, you’ll be asked to write a SQL statement, check a query for mistakes, or determine the result of a given query. Below, I’ve copied ten SQL exercises of the types you might do on a technical job interview. They are sorted from easy to hard. 1: Students in the Same City as Their School Write an SQL statement that lists school names, student names, and their cities only if the school and the student are in the same city and the student is not from New York. Table: school school_id school_name city ----------- ------------------ ---------- 1 Stanford Stanford 2 University of Cali San Francisco 3 Harvard University New York 4 MIT Boston 5 Yale New Haven Table: student student_id student_name city school_id ----------- ------------ ---------- ----------- 1001 Peter Brebec New York 1 1002 John Goorgy San Francisco 2 2003 Brad Smith New York 3 1004 Fabian Johns Boston 5 1005 Brad Cameron Stanford 1 1006 Geoff Firby Boston 5 1007 Johnny Blue New Haven 2 1008 Johse Brook Miami 2 Solution: SELECT school_name, student_name, school.city, student.city FROM school JOIN student ON school.school_id = student.school_id WHERE school.city = student.city AND student.city != 'New York'; 2: Courses and Their Maximum Scores Write an SQL statement that lists student names, course names, course lecturers, and the max amount of points for all courses except Computer Science and any courses with a max score between 100 and 200. Table: course course_id course_name max_score lecturer ---------- ---------- ---------- ----------- 11 Math 130 Christena Solem 12 Computer Science 150 Jaime Pille 13 Biology 300 Carrol Denmark 14 Geography 220 Yuette Galang 15 Physics 110 Colton Rather 16 Chemistry 400 Nan Mongeau Table: student student_id student_name city course_id ----------- ------------ ---------- ----------- 2001 Thurman Thorn New York 11 2002 Sharda Clemens San Francisco 12 2003 Buck Elkins New York 13 2004 Fabian Johns Boston 15 2005 Brad Cameron Stanford 11 2006 Sofia Roles Boston 16 2007 Rory Pietila New Haven 12 Solution: SELECT student_name, course_name, lecturer, max_score FROM course JOIN student ON course.course_id = student.course_id WHERE course_name != 'Computer Science' AND max_score BETWEEN 100 AND 200; 3: Find the Error in the Query What is wrong with this SQL query? SELECT Id, name, YEAR(BillingDate) AS Year FROM Records WHERE Year >= 2010 Answer: You can't use an alias in the WHERE clause. You have to use the full expression, YEAR(BillingDate). SELECT id, name FROM students WHERE grades = (SELECT MAX(grades) FROM students GROUP BY subject_id;); Answer: There is an additional semicolon ; within the subquery. You should only put the semicolon at the end of the query. 4: Find the Result of the Query Given the following tables … Table: worker id name --- ----------- 1 Guillermo Sparks 2 Gene Roberts 3 Ally Jones 4 Bryant Summers 5 Candice Green Table: departments id name manager_id --- ------------- --------- 1 Financial 3 2 Strategy 5 3 IT 1 4 Marketing NULL … What will be the result of the query below? SELECT name FROM worker WHERE id NOT IN (SELECT manager_id FROM departments); Answer: name ----------- Gene Roberts Bryant Summers 5: Find the Highest Salary in Each Department The employee table has the following attributes: name, dept_id, and salary. Write a query that shows the highest salary in each department. Answer: SELECT dept_id, MAX(salary) FROM employee GROUP BY dept_id; 6: Find Current Date Write an SQL query that displays the current date. Answer: SELECT current_date; 7: Age of customers Write a query to calculate the current age each customer in a Customers table. Answer: SELECT CustomerID, FirstName, LastName, FLOOR(DATEDIFF(CURDATE(), BirthDate) / 365) AS Age FROM Customers; 8: Find Duplicate Rows Write an SQL query to find duplicate rows in two tables (employee and workers). Answer: SELECT * FROM employee INTERSECT SELECT * FROM workers; 9: Courses with Three or More Students Write a query that lists courses’ subject names and the number of students taking the course only if the course has three or more students enrolled. Table: subject subject_id subject_name max_score lecturer ---------- ---------- ---------- ----------- 11 Math 130 Christena Solem 12 Computer Science 50 Jaime Pille 13 Biology 300 Carrol Denmark 14 Geography 220 Yuette Galang 15 Physics 110 Colton Rather 16 Chemistry 400 Nan Mongeau Table: student student_id student_name city subject_id ----------- ------------ ---------- ----------- 2001 Thurman Thorn New York 11 2002 Sharda Clemens San Francisco 12 2003 Buck Elkins New York 13 2004 Fabian Johns Boston 15 2005 Brad Cameron Stanford 11 2006 Sofia Roles Boston 16 2007 Rory Pietila New Haven 12 2008 Cicely Weish Tulsa 14 2011 Richard Curtin Boston 11 2012 Kassy Ledger Stanford 11 2013 Henry Ledger Miami 13 2014 Darius Fidzberg San Francisco 12 2015 Darcey Fiorillo Chicago 14 Answer: SELECT subject_name, COUNT(student_id) FROM subject JOIN student ON subject.subject_id = student.subject_id GROUP BY subject_name HAVING COUNT(student_id) >= 3; 10: Average Age of Employees in Each Company Write a query that displays the average age of workers in each company. The result should show the name of the company and the average age of the company’s workers. Table: company id name ------ ------------- 1 Amazon 2 Samsung 3 LG 4 Kia 5 Lenovo Table: workers id name age company_id ------- ------------------------- ---------- ------------- 1 Derick Davison 20 5 2 Bari Becnel 50 1 3 Maddie Mueller 33 2 4 Eva Emrich 23 1 5 Katelyn Kunze 55 3 6 Hue Hover 26 2 7 Lucas Lout 57 4 8 Granville Grande 30 5 9 Robt Rude 39 3 10 Lisha Lewin 25 4 11 Aron Atlas 52 3 12 Chester Coddington 44 4 13 Diedre Dominy 31 1 14 Evie Edgell 35 2 15 Judy Johanson 36 5 Answer: SELECT company.name, AVG(age) FROM company JOIN workers ON company.id = workers.company_id GROUP BY company.name; Ace Your Interview with the SQL Practice Track Preparing for SQL job interviews requires more than just understanding theory—you need hands-on practice to tackle real-world challenges. The SQL Practice Track at LearnSQL.com is designed to strengthen your query-writing skill. The track offers over 1,000 coding challenges across 10 courses, covering fundamental topics such as SELECT statements, JOIN, aggregation with GROUP BY and HAVING, and subqueries, including correlated subqueries. Each course provides real-life problem-solving scenarios, allowing you to apply your knowledge in practical contexts. By engaging with these exercises, you'll build confidence, improve your proficiency, and be well-prepared to showcase your SQL skills in any interview setting. Give yourself the competitive edge needed to succeed. Tags: sql interview questions