Databases are really the backbone of the Software Development and SQL is the standard language used to query a database. In this tutorial series, you’ll learn Top SQL Query Interview Questions and Answers with useful SQL learning Courses, Resources and PDFs (at the end) as well.
Note*** : If you’ve directly landed on this guide, you can check out the Part 1 : SQL Query Interview Questions and Answers to set up the sample Databases, Tables, and practice these Tricky SQL queries for Interview.
These SQL Query Interview Questions will definitely help any Fresher or Intermediate level developer to practice by yourself and prepare the advanced level queries to crack any Technical Interview of top MNCs like Amazon, Flipkart, Uber, and others.
Let’s begin : Step by Step –
SQL Query Interview Questions For Practice
Q16. Write an SQL query to fetch the name of the TRADER who earns the highest INCOME?
SELECT ID, FIRST_NAME, INCOME FROM TRADER WHERE INCOME = (SELECT MAX(INCOME) FROM TRADER);
The MAX() function returns the maximum value within the range that you provide. In the MAX() function, you can provide a column name of a table or a set of values to find the greatest number in it.
Q17. Write an SQL query to print details of the Traders whose FIRST_NAME ends with ‘a’?
SELECT trader_id AS ID, first_name, income FROM trader WHERE first_name LIKE '%a';
The LIKE keyword in SQL is basically a logical operator that tests whether a string contains a specified pattern or not. You can use it in WHERE clause of the SELECT, DELETE, and UPDATE statements to filter the data based on the pattern that you’ve provided.
In MySQL, there are 2 pattern specifiers: Percentage % and Underscore _ .
- The percentage ( % ) wildcard is used to match any string of zero or more characters.
- The underscore ( _ ) wildcard is used to match a particular character.
The LIKE command is a powerful command in SQL, and you must know- How to use LIKE with % and _ wildcards. The LIKE command is supported by all the major databases such as MySQL, Oracle, Microsoft SQL, PostgreSQL, and SQLite. Also, there is Become Junior Database Analyst with Oracle course on Udemy, which you should check if you really want to be an Expert Database Analyst.
Q18. Write an SQL query to print details of the Traders whose FIRST_NAME ends with ‘d’ and contains five alphabets?
SELECT trader_id AS ID, first_name, income FROM trader WHERE first_name LIKE '____d';
In the above query, I’ve used the LIKE pattern with Underscore _ wildcard pattern specifier.
Q19. Write an SQL query to print details of the Traders whose Income lies between 10000 and 200000?
SELECT trader_id AS ID, first_name, income FROM trader WHERE income BETWEEN 10000 AND 200000;
In the above query, I’ve used the BETWEEN operator, which’s also a Logical operator that allows us to specify whether a value in a range or not. It is often used in the WHERE clause of the UPDATE, SELECT, and DELETE statements.
Q20. Write an SQL query to print details of the Traders who have started trading in April 2014?
SELECT * FROM trader WHERE Year(starting_date) = 2014 AND Month(starting_date) = 4;
These are must to know type of SQL Query Interview Questions and Answers. In above query, I’ve used YEAR() and MONTH() function, Let’s Learn more about these-
- We can pass a date argument in the YEAR() function and it returns the year of the date.
- In MONTH() function, we specify a date value and it returns an integer that represents the month of a specified date.
Q21. Write an SQL query to fetch the number of Traders trading in ‘West_Zone’?
SELECT Count(*) AS Total_Traders FROM trader WHERE area_zone = 'West_Zone';
Q22. Write an SQL query to fetch Trader names with income >= 75000 and <= 100000?
SELECT Concat(first_name, ' ', last_name) AS Trader_Name, income FROM trader WHERE trader_id IN (SELECT trader_id FROM trader WHERE income BETWEEN 75000 AND 100000);
These are some of the core level SQL Query Interview Questions and Answers which are asked to check, whether the candidate is aware of using nested queries or not. In this solution, I’ve used a subquery, which is a query nested within another SQL query, such as UPDATE, INSERT, SELECT, or DELETE.
These subqueries are also known as Inner or Nested Queries, while the query that contains the subquery is called an Outer Query. You must know how to use sub queries efficiently. Here’s the Complete SQL from Zero to Hero course on Udemy that you should go with if you really want to become an expert in Database and SQL programming.
The Sub-Queries are supported by all the Trending databases currently being used in Industry such as- MySQL, Oracle, Microsoft SQL, PostgreSQL and SQLite. Check these courses and Official documentation to learn – How a sub-query reduces the overhead of a database and similar other concepts to be better in SQL.
Q23.Write an SQL query to fetch the total no. of Traders for each Zone in a descending order.?
SELECT area_zone, Count(trader_id) AS No_Of_Traders FROM trader GROUP BY area_zone ORDER BY no_of_traders DESC;
In the above query, I’ve used the GROUP BY clause, which is used to group a set of records into a set of summary rows by expressions or the values of fetched columns.
For each group, the GROUP BY clause returns only One row. In simple words, you can use the GROUP BY clause to reduces the number of rows in the result set. Also, the Aggregate Functions such as SUM(), AVG(), MAX(), MIN(), and COUNT() provides information about each group when used with SELECT statements.
Q24. Write an SQL query to print details of the TRADERs performing well and reached Top_Level?
SELECT DISTINCT TR.first_name, TL.the_level FROM trader TR INNER JOIN trader_level TL ON TR.trader_id = TL.trader_ref_id AND TL.the_level = 'Top_Level';
In above SQL Query, I’ve used the INNER JOIN which is used to match each row in one table with every row in other tables. It allows us to query multiple rows that have columns from both tables.
- It is an optional clause of the SELECT statement.
- INNER JOIN appears immediately after the FROM clause.
Q25. Write an SQL query to fetch duplicate records having matching data in some fields of a table?
SELECT the_level, affected_from, Count(*) FROM trader_level GROUP BY the_level, affected_from HAVING Count(*) > 1;
WHERE command can not be used with aggregate functions such as COUNT(*), so we have used the HAVING clause.
Q26. Write an SQL query to show only odd rows from a table?
SELECT first_name, area_zone FROM trader WHERE Mod (trader_id, 2) <> 0;
In above query, I’ve used the MOD() function which returns the remainder of a number divided by another number. Syntax of the MOD() function is like:
We provide two arguments when calling the MOD() function:
- dividend : It’s a number or a numeric expression to divide.
- divisor : It’s a number or a numeric expression used to divide the dividend.
There are useful functions such as- RTRIM( ), TRUNC( ), and EXTRACT( datatime ) provided by all the major databases including MySQL, Oracle, Microsoft SQL Server, PostgreSQL and SQLite. Here’s a helpful course on Udemy- SQL : Beginner to Expert to learn in-depth about the CRUD operations, Joins, SQL functions, Triggers and all other major-minor Databases concepts. Check the above courses and their Official documentations, and You will definitely learn a lot from them.
Q27. Write an SQL query to show only even rows from a table?
SELECT first_name, area_zone FROM trader WHERE Mod (trader_id, 2) = 0;
Q28. Write an SQL query to clone a new table from another table?
-- STEP 1: CREATE TABLE copy_of_traders LIKE trader; -- STEP 2: ALTER TABLE copy_of_traders DISABLE KEYS; -- STEP 3: INSERT INTO copy_of_traders SELECT * FROM trader; -- STEP 4: ALTER TABLE copy_of_traders ENABLE KEYS;
We can clone a table in any database. All we need to do is to create a dummy table using CREATE TABLE command followed by SELECT * statement to copy all data. But, be careful when performing this operation at big tables.
Q29. Write an SQL query to fetch the list of employees with the same Income?
SELECT DISTINCT W.trader_id, W.first_name, W.income FROM trader W, trader W1 WHERE W.income = W1.income AND W.trader_id != W1.trader_id;
Q30. Write an SQL query to show the top n (say 10) records of a table?
SELECT * FROM trader ORDER BY income DESC LIMIT 10;
SELECT * FROM trader WHERE ROWNUM < 11 ORDER BY income DESC;
That’s all for now. With your support, We have practiced a number of Fundamental SQL concepts including Creating Tables, Inserting Records, GROUP BY, ORDER BY, INNER JOIN queries, and more.
However, A lot of other concepts such as Full Joins, Outer Joins, Triggers, will be covered in the upcoming parts of this series. But, to understand all those SQL Queries, you must have a better command in SQL with at least 1 Databese.
Here are some PDFs and better Courses for you, that I’ve picked up based upon Important factors such as – Latest concepts, Reviews of happy students, Hours of lectures and more. So, go ahead, choose anyone and Start Learning in-depth from today :
Useful PDFs :
Better Resources (100%) :
1) MySQL : Go from SQL Beginner to Expert
2) Oracle : 11g/12c version – Become Junior DBA
3) Microsoft SQL for Beginners (Free till Feb, 21)
4) PostgreSQL : Complete SQL from Zero to Hero
6) MongoDB : The Developer’s Guide 2121
7) ElasticSearch : Learn from Scratch
8) MariaDB : Become an App Developer
I hope, this article helped you to learn a bit more about- SQL and Databases? If you have any doubt, query or suggestion, then do comment or send me a direct email via Google, I’ll try my best to give you the solution within a day.
P.S.*** : If you are really want to be an expert in SQL programming and Database domain, you can go with the MySQL – Beginner to Expert course on Udemy.
Found Helpful? Just share it!
I’ve put so much effort into writing this article to provide value to the programming community. If you found this helpful? Smash the Yellow Icon, Copy its Link, and share with social media friends, It’ll be very helpful for all. Thanks!!!