UHG
Search
Close this search box.

5 Complex SQL Queries to Nail Your Job Interview

Hand-picked for the data fans out there!

Share

As we know, SQL (Structured Query Language) is a vital tool in a data scientist’s toolbox. Of course, mastering SQL is essential for interviews, but a good understanding of SQL by solving complex queries will help you a great deal in different phases of your career.

Here are five complex solutions for all the data enthusiasts out there! 

Note: For the following queries, extensive data tables will be available that need to be fed into an SQL editor. With their respective inputs, the output is generated.   

1. Query to fetch ‘EmpId’ and ‘FullName’ of the employees working under the Manager with id – ’03’.

Answer: For this, the EmployeeDetails table can be used to fetch the employee details with a where clause for the Manager.

(Input)

SELECT EmpId, FullName

FROM EmployeeDetails

WHERE ManagerId = 03;

Here’s an example.

2. Query to fetch the first record from a table

Answer: There are two ways to ‘fetch the first record’, 

(Input)

select * from Student where RowID = select min(RowID) from Student;

The second method is by printing just one (first) row of the table:

select * from Student where Rownum = 1;

Here’s an example. 

3. Show only common records between two tables.

Answer: Feed the following input in the editor:

(Input)

Select * from Student;

Intersect

Select * from StudentA;

Here’s an example. 

4. Query to find the record in Table A, not Table B, without using the NOT IN operator.

Consider two tables:

Answer: We can use the MINUS operator for Oracle and EXCEPT for SQL Server.

The query will be as follows:

(Input)

SELECT * FROM Table_A

MINUS

SELECT * FROM Table_B

Here’s an example. 

5. Differentiate between UNION and UNION ALL

Answer: The major difference between UNION and UNION ALL is:

UNION eliminates duplicate records. On the other handUNION, ALL does not.

For example, consider two tables:

UNION of A and B = 20, 30, 25

UNION ALL of A and B = 20, 25, 25, 30

The performance of UNION ALL is considered better than UNION since UNION ALL does not require additional work of removing duplicates.

Note: The above queries enhance your knowledge as an aspiring data science professional (from data architects to software engineers) and add to your existing skills. As a result, SQL is one of the most in-demand skills among all jobs in the domain, appearing in 42.7% of all data science job postings. 

📣 Want to advertise in AIM? Book here

Picture of Tasmia Ansari

Tasmia Ansari

Tasmia is a tech journalist at AIM, looking to bring a fresh perspective to emerging technologies and trends in data science, analytics, and artificial intelligence.
Related Posts
19th - 23rd Aug 2024
Generative AI Crash Course for Non-Techies
Upcoming Large format Conference
Sep 25-27, 2024 | 📍 Bangalore, India
Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

Flagship Events

Rising 2024 | DE&I in Tech Summit
April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore
Data Engineering Summit 2024
May 30 and 31, 2024 | 📍 Bangalore, India
MachineCon USA 2024
26 July 2024 | 583 Park Avenue, New York
MachineCon GCC Summit 2024
June 28 2024 | 📍Bangalore, India
Cypher USA 2024
Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA
Cypher India 2024
September 25-27, 2024 | 📍Bangalore, India
discord-icon
AI Forum for India
Our Discord Community for AI Ecosystem, In collaboration with NVIDIA.