20th Nov 2024 12 minutes read SQL Interview Questions for Software Testers Tihomir Babic SQL Interview Questions Table of Contents 1. What Is the Basic Structure of an SQL Query? 2. How Would You Modify Data in a Database? 3. Find User Accounts That Satisfy Certain Criteria 4. Sort the Output Values 5. Find the Highest and Lowest Values 6. What Is a JOIN in SQL, and Which SQL JOINs Are You Familiar With? 7. Output Matching Data From Two Tables 8. Categories with Stock Above 100 9. What Is a Primary Key in a Database? 10. What Database Engines Do You Work With? Ready to Ace These SQL Software Testing Questions? SQL is a crucial skill for software testers. Otherwise, how would they get the data to test? Interviewers for these jobs may ask you several SQL software testing questions. So, if you’re preparing for a job interview, you’ll need to know how to answer these ten common SQL questions. Why do software testing interview questions include SQL? Well, software testers commonly use Structured Query Language in their daily tasks. For example, they use it to: Validate data between apps and databases. Find user accounts that satisfy the criteria they need to test. Retrieve test data from databases Check errors in data saved in databases. Verify the business logic implemented in the database. The common word here is ‘database’, which is SQL’s domain; it was designed to handle data in databases. Because of its importance in the day-to-day tasks of software testing, SQL also commonly features in software testing interview questions. The questions usually test both theoretical and practical SQL knowledge. The best way to prepare for the interview is to read about the SQL concepts used by software testers and solve as many coding challenges involving those topics as possible. So, yes, finding more articles like this one or other lists of SQL interview questions is strongly recommended. Also, you may want to take courses that focus on the knowledge you need, such as our SQL Basics course. Its topics range from retrieving data from a database, creating basic reports, and filtering data using WHERE to working with JOINs, GROUP BY, subqueries, and set operations. If you still haven’t mastered these topics, this course’s 100+ coding exercises will help you get the skills you need. I’ll focus this article on SQL concepts that are important for software testing. If you’re not already familiar with SQL, I advise you to have our free SQL Basics Cheat Sheet nearby for quick reference. 1. What Is the Basic Structure of an SQL Query? Answer: The basic SQL query is a SELECT statement with a FROM clause, as shown below: SELECT column_1, column_2, … FROM table_name; An SQL query can’t work without these two clauses: SELECT – Lists the column(s) to be fetched from the table. FROM – Specifies the table from which the column(s) in SELECT will be fetched. However, an SQL query very often includes some other (optional) clauses. A slightly less basic query would look like this: SELECT column_1, column_2, …, aggregated_column FROM table_name WHERE column_1 condition GROUP BY column_1, column_2, … HAVING aggregated_column condition ORDER BY column_1 [ASC|DESC]; Here’s an explanation of each additional clause. WHERE: Checks the values in the specified column(s) and returns rows with values that satisfy the condition. Used for filtering data before GROUP BY: Organizes data by values in the specified columns. HAVING: Checks the values in the specified aggregated column(s) and returns those that satisfy the condition. Used for filtering data after ORDER BY: Sorts the output by specific columns in ascending (from the lowest to the highest numerical values; alphabetically for string values; from the oldest to the newest date) or descending order (from the highest to the lowest numerical values; reverse alphabetically for string values; from the newest to the oldest date). 2. How Would You Modify Data in a Database? Answer: Testers will sometimes modify data in a database to create a specific testing condition. So questions about data modification are pretty common in software tester interviews. You can insert data into a database with the INSERT statement. You change existing data in a database with the UPDATE statement. You remove data from a database with the DELETE statement. The INSERT statement inserts one or more new rows of data into a table using this syntax: INSERT INTO table_name (column_1, column_2, column_3, …) VALUES (value_1, value_2, value_3, …); The UPDATE statement modifies existing values in a database table column. Here’s the syntax: UPDATE table_name SET column_name = value WHERE condition; The DELETE statement erases one or multiple rows from the table using this syntax: DELETE FROM table_name WHERE condition; For additional popular software testing interview question topics, you may want to work on some of these 22 SQL Command Practices before the interview. 3. Find User Accounts That Satisfy Certain Criteria Problem Description: Imagine that you work with the app_users table given below and want to test it using some of its data. This data should include only: Active users who Registered in 2024, and Have more than $100 in their account. How would you go about writing the query to find those users? First, let’s look at a sample of the data: idusernameemailregistration_dateuser_typestatusaccount_balance 1mjohnsonmjohnson@gmail.com2024-02-08adminactive25.00 2sstevenssstevens@gmail.com2023-05-08regular_useractive528.49 3bkingbking@gmail.com2023-09-12regular_userinactive0.00 4adedicadedic@gmail.com2024-07-04adminactive184.22 5blavetteblavette@gmail.com2024-11-03regular_useractive208.50 Solution: To solve the problem you’re given, you need to write the following query that has three conditions in the WHERE: SELECT id, username, email, registration_date, user_type, status, account_balance FROM app_users WHERE status = 'Active' AND registration_date > '2023-12-31' AND account_balance > 100; This query selects all the columns from the table app_users. Then, it uses WHERE and three filtering conditions with the comparison and logical operators. The first condition is that the status is active, so the values in the column status have to equal (=; the comparison operator) the value 'active'. Note: This filters text data, so the value has to be written in single quotes ('). The second filtering condition is that the registration date has to be later than ( the > comparison operator) 2023-12-31, i.e., the user registered in 2024. Note: This filters dates, which also have to be written in single quotes. Finally, the third condition checks that the user’s account balance is above 100. This, again, uses the greater than (>) comparison operator. Here, the value is written without single quotes because it’s a numerical value. All three conditions have to be satisfied simultaneously, so we ‘chain’ all three conditions in WHERE by using two AND logical operators. This is a common interview question, so I advise you to practice this topic using these 20 SQL WHERE Practice Exercises for Beginners. Output: The output shows two users that satisfy all three criteria. idusernameemailregistration_dateuser_typestatusaccount_balance 4adedicadedic@gmail.com2024-07-04adminactive184.22 5blavetteblavette@gmail.com2024-11-03regular_useractive208.50 4. Sort the Output Values Problem Description: The interviewer might ask you to sort the output you get in the previous question from the most recent to the oldest registration dates. Solution: To do so, you need an ORDER BY clause at the end of the query. SELECT id, username, email, registration_date, user_type, status, account_balance FROM app_users WHERE status = 'active' AND registration_date > '2023-12-31' AND account_balance > 100 ORDER BY registration_date DESC; To sort the output by registration date, simply add an ORDER BY clause with the registration_date column to the end of the previous query. You need to order from the newest to the oldest registration dates – i.e. sorting in descending order – so don’t forget to write DESC after the column name. Output: Here’s the sorted output: idusernameemailregistration_dateuser_typestatusaccount_balance 5blavetteblavette@gmail.com2024-11-03regular_useractive208.50 4adedicadedic@gmail.com2024-07-04adminactive184.22 5. Find the Highest and Lowest Values Problem Description: A very common SQL interview question for QA testers involves finding the highest and lowest values in a dataset. Specifically, let’s say you’re asked to return the lowest and the highest amount on all users’ accounts by querying the same table as earlier. When you have these values, you can check the data in the app to see if there are some accounts that are lower or higher than that. Solution: To solve this problem, you need two aggregate functions: MIN() and MAX(). Their syntax is simple; just write the column name from which you want the minimum/maximum value in the function parentheses. SELECT MIN(account_balance) AS minimum_account_balance, MAX(account_balance) AS maximum_account_balance FROM app_users; To make the output more readable, we can create aliases for the aggregated columns by writing the AS keyword followed by the column alias. Output: Here’s the output: minimum_account_balancemaximum_account_balance 0528.49 6. What Is a JOIN in SQL, and Which SQL JOINs Are You Familiar With? Answer: JOIN is a clause used for combining two or more tables in SQL. The syntax is shown below: SELECT table_1.column_1, table_2.column_2, … FROM table_1 JOIN table_2 ON table_1.common_column = table_2.common_column; The first joined table is listed in FROM, and the second in JOIN. The tables are joined on the common column and this joining condition is written in the ON clause. In SQL, there are five JOINs: (INNER) JOIN LEFT (OUTER) JOIN RIGHT (OUTER) JOIN FULL (OUTER) JOIN CROSS JOIN INNER JOIN returns only the matching rows from the joined tables. LEFT JOIN returns all the rows from the first (left) table and the matching rows from the second (right) table. The non-matched values from the right table are shown as NULL. RIGHT JOIN is the mirror image of LEFT JOIN. It returns all the rows from the second (right) table and the matching rows from the first (left) table. The non-matched values from the left table are shown as NULL. FULL JOIN is a combination of LEFT JOIN and RIGHT JOIN; it returns all the rows from both left and right tables. The non-matched values from either table are shown as NULL. CROSS JOIN combines every row of one table with all the rows of the other table, resulting in a Cartesian product (i.e. every possible combination of rows). All this is crucial SQL knowledge; you can learn more about it with these 10 SQL JOIN interview questions and answers. 7. Output Matching Data From Two Tables Problem Description: Once again, you’re given the app_users table. You also have the session_data table, which shows info about each session in the app. A snippet of this table is shown below: idapp_user_idsession_startsession_end 112024-10-30 6:05:282024-10-30 6:25:52 222024-10-30 12:58:492024-10-30 14:07:07 312024-10-31 4:22:122024-10-31 5:12:43 422024-10-31 9:51:122024-10-31 9:58:24 552024-11-03 21:36:392024-11-03 23:18:25 Your task is to list users’ IDs, usernames and their sessions’ start and end times so you can test this data. Don’t include the usernames who didn’t have any sessions in the app. Solution: The problem requires using data from both tables, so you’ll have to join them. However, which join type should you use? The answer is INNER JOIN. Why? Because it returns only matching rows, i.e., only the users that can be found in both tables. If they can be found in both tables, then it means they have at least one session recorded in the table session_data. SELECT au.id AS user_id, au.username, sd.session_start, sd.session_end FROM app_users au JOIN session_data sd ON au.id = sd.app_user_id; The first joined table is referenced in FROM and is given the alias au. (Aliases are not mandatory, but it helps to shorten the table name when you need to reference it again). To join the second table, add the keyword JOIN (or INNER JOIN; they mean the same thing in SQL) and write the table’s name. The tables are joined on the common column. In this case, it’s id from the first table and app_user_id from the second table. Now, you can select all the necessary columns from both tables. To easily understand which column is coming from which table, preface the column name with the table name (or table alias) followed by a dot: table_name.row_name. Output: The output shows five sessions across three users: user_idusernamesession_startsession_end 1mjohnson2024-10-30 6:05:282024-10-30 6:25:52 2sstevens2024-10-30 12:58:492024-10-30 14:07:07 1mjohnson2024-10-31 4:22:122024-10-31 5:12:43 2sstevens2024-10-31 9:51:122024-10-31 9:58:24 5blavette2024-11-03 21:36:392024-11-03 23:18:25 8. Categories with Stock Above 100 Problem Description: You’re given the table products to solve this software testing interview question. Here’s a snapshot of it: idproduct_nameproduct_categorystock_quantity 1Wireless earbudsElectronics518 2Vacuum cleanerAppliances208 3TreadmillSports12 4USB-C cableElectronics121 5DishwasherAppliances80 6Exercise bikeSports17 7Wireless boomboxElectronics94 8Washing machineAppliances103 9Yoga matSports54 You want to output the product categories and their total stock quantity, but only for categories with total stock above 100. You’ll need this to check if the data is correctly recorded in the inventory app. Solution: The solution uses the SUM() aggregate function to sum the total stock quantity. To get this calculation by product category, select the product_category column and group the output by the same column in GROUP BY. To show categories with total stock above 100 means the filtering has to be done after the aggregation. Because of that, you must use HAVING for filtering. (Remember, WHERE is only for filtering before the aggregation). In HAVING, you’ll again write the aggregate column that calculates the total stock quantity, with the condition that only values above 100 are included in the output. Output: There are two product categories that satisfy the HAVING condition: product_categorytotal_stock_quantity Appliances391 Electronics733 9. What Is a Primary Key in a Database? Answer: A primary key is a constraint created on the column, uniquely identifying each row in the table by not accepting duplicate values and NULLs. It has several important purposes: Ensuring there is no duplicate or missing data in that column. Providing a way to uniquely identify each row in the table. Establishing a relationship with another table via a foreign key. Ensuring row-level accessibility of a table’s data. You can learn more about keys in our article Why Use Primary Keys and Foreign Keys in Databases?. 10. What Database Engines Do You Work With? Problem Description: You might be asked what database engines you’ve worked with. This somewhat strange and easy question has the potential to surprise you if you never gave thought to it. Answer: Not all SQL database engines are the same. It does matter which database engine(s) you use, especially if those are the engines you will use at a prospective job. Make sure that you’re aware of the most popular database engines. This is important not only because of the technical differences between the tools but also because of the differences in the SQL dialects those engines use. Most importantly, know which of those engines you used in your previous projects or roles. The interviewer may be interested in knowing which database engines you’re familiar with. Ready to Ace These SQL Software Testing Questions? These ten interview questions have given you a good idea of what SQL interview questions for software testers look like. Expect to be given a mix of theory questions and hands-on SQL tasks. The topics we’ve discussed are basic SQL knowledge. If you want to do well on your SQL interview, make sure you’re comfortable with everything I mentioned in the article. To consolidate your fundamental SQL knowledge, I strongly recommend trying out our SQL Basics and How to INSERT, UPDATE, and DELETE Data in SQL courses, then reviewing everything in the SQL Practice track. Tags: SQL Interview Questions