MLStackMLSCCafé
 
 
Sign in with GoogleSign in with Google. Opens in new tab
Master Your ML & AIAI Interview
2103 Curated Machine Learning, Data Science, AI & LLMs Interview Questions
Answered To Get Your Next Six-Figure Job Offer

23 Advanced SQL Interview Questions (SOLVED) Data Scientists Must Know

SQL is the most important skill for a data scientist. Yes. It’s true. Relational Database Management is an important part of Data Science and SQL is the most-used language in data science, according to the 10,000+ data professionals who responded to StackOverflow's 2020 survey. Follow along and check the 23 most common and advanced SQL Interview Questions Data Scientists and Machine Learning Engineers must be prepared before their next ML or Data Science Interview.

Q1: 
What is Normalisation?

Answer

Normalization is basically to design a database schema such that duplicate and redundant data is avoided. If the same information is repeated in multiple places in the database, there is the risk that it is updated in one place but not the other, leading to data corruption.

There is a number of normalization levels from 1. normal form through 5. normal form. Each normal form describes how to get rid of some specific problem.

By having a database with normalization errors, you open the risk of getting invalid or corrupt data into the database. Since data "lives forever" it is very hard to get rid of corrupt data when first it has entered the database.


Having Machine Learning, Data Science or Python Interview? Check 👉 29 Databases Interview Questions

Q2: 
What is the difference between Data Definition Language (DDL) and Data Manipulation Language (DML)?

Answer
  • Data definition language (DDL) commands are the commands which are used to define the database. CREATE, ALTER, DROP and TRUNCATE are some common DDL commands.

  • Data manipulation language (DML) commands are commands which are used for manipulation or modification of data. INSERT, UPDATE and DELETE are some common DML commands.


Having Machine Learning, Data Science or Python Interview? Check 👉 52 SQL Interview Questions

Q3: 
Define ACID Properties

  • Atomicity: It ensures all-or-none rule for database modifications.
  • Consistency: Data values are consistent across the database.
  • Isolation: Two transactions are said to be independent of one another.
  • Durability: Data is not lost even at the time of server failure.

Having Machine Learning, Data Science or Python Interview? Check 👉 29 Databases Interview Questions

Q4: 
Duplicate Emails Challenge

Problem

Write a SQL query to find all duplicate emails in a table named Person.

Table: Customers.

+----+---------+
| Id | Email   |
+----+---------+
| 1  | a@b.com |
| 2  | c@d.com |
| 3  | a@b.com |
+----+---------+

For example, your query should return the following for the above table:

+---------+
| Email   |
+---------+
| a@b.com |
+---------+

Note: All emails are in lowercase.

Answer
Source: gdcoder.com

Since all emails are in lowercase we can simply group by email and print those that have a count > 1.

SELECT EMAIL
FROM PERSON
GROUP BY EMAIL
HAVING COUNT(*)>1

Having Machine Learning, Data Science or Python Interview? Check 👉 52 SQL Interview Questions

Q5: 
Find duplicate values in a SQL table

Mid 
Problem

We have a table

ID   NAME   EMAIL
1    John   asd@asd.com
2    Sam    asd@asd.com
3    Tom    asd@asd.com
4    Bob    bob@asd.com
5    Tom    asd@asd.com

I want is to get duplicates with the same email and name.

Answer

Simply group on both of the columns:

SELECT
    name, email, COUNT(*) as CountOf
FROM
    users
GROUP BY
    name, email
HAVING 
    COUNT(*) > 1

Having Machine Learning, Data Science or Python Interview? Check 👉 2103 Interview Questions

Q6: 
What are the difference between Clustered and a Non-clustered index?

Answer
  • With a Clustered index the rows are stored physically on the disk in the same order as the index. Therefore, there can be only one clustered index. A clustered index means you are telling the database to store close values actually close to one another on the disk.
  • With a Non Clustered index there is a second list that has pointers to the physical rows. You can have many non clustered indices, although each new index will increase the time it takes to write new records.
  • It is generally faster to read from a clustered index if you want to get back all the columns. You do not have to go first to the index and then to the table.
  • Writing to a table with a clustered index can be slower, if there is a need to rearrange the data.

Having Machine Learning, Data Science or Python Interview? Check 👉 29 Databases Interview Questions

Q7: 
What is Denormalization?

Answer

It is the process of improving the performance of the database by adding redundant data.


Having Machine Learning, Data Science or Python Interview? Check 👉 29 Databases Interview Questions

Q8: 
What is Collation?

Answer

In database systems, Collation specifies how data is sorted and compared in a database. Collation provides the sorting rules, case, and accent sensitivity properties for the data in the database.

For example, when you run a query using the ORDER BY clause, collation determines whether or not uppercase letters and lowercase letters are treated the same.


Having Machine Learning, Data Science or Python Interview? Check 👉 52 SQL Interview Questions

Q9: 
What is the difference between INNER JOIN, OUTER JOIN, FULL OUTER JOIN?

Answer

An inner join retrieve the matched rows only.

Whereas an outer join retrieve the matched rows from one table and all rows in other table ....the result depends on which one you are using:

  • Left: Matched rows in the right table and all rows in the left table
  • Right: Matched rows in the left table and all rows in the right table or
  • Full: All rows in all tables. It doesn't matter if there is a match or not

Inner Join

Retrieve the matched rows only, that is, A intersect B.

SELECT *
FROM dbo.Students S
INNER JOIN dbo.Advisors A
    ON S.Advisor_ID = A.Advisor_ID

Left Outer Join

Select all records from the first table, and any records in the second table that match the joined keys.

SELECT *
FROM dbo.Students S
LEFT JOIN dbo.Advisors A
    ON S.Advisor_ID = A.Advisor_ID

Full Outer Join

Select all records from the second table, and any records in the first table that match the joined keys.

SELECT *
FROM dbo.Students S
FULL JOIN dbo.Advisors A
    ON S.Advisor_ID = A.Advisor_ID

Having Machine Learning, Data Science or Python Interview? Check 👉 52 SQL Interview Questions

Q10: 
What is the difference between JOIN and UNION?

Answer

UNION puts lines from queries after each other, while JOIN makes a cartesian product and subsets it -- completely different operations. Trivial example of UNION:

mysql> SELECT 23 AS bah
    -> UNION
    -> SELECT 45 AS bah;
+-----+
| bah |
+-----+
|  23 |
|  45 |
+-----+
2 rows in set (0.00 sec)

similary trivial example of JOIN:

mysql> SELECT * FROM
    -> (SELECT 23 AS bah) AS foo
    -> JOIN
    -> (SELECT 45 AS bah) AS bar
    -> ON (33=33);
+-----+-----+
| foo | bar |
+-----+-----+
|  23 |  45 |
+-----+-----+
1 row in set (0.01 sec)

Having Machine Learning, Data Science or Python Interview? Check 👉 52 SQL Interview Questions

Q11: 
What is the difference between UNION and UNION ALL?

Answer

UNION removes duplicate records (where all columns in the results are the same), UNION ALL does not.

There is a performance hit when using UNION instead of UNION ALL, since the database server must do additional work to remove the duplicate rows, but usually you do not want the duplicates (especially when developing reports).

UNION Example:

SELECT 'foo' AS bar UNION SELECT 'foo' AS bar

Result:

+-----+
| bar |
+-----+
| foo |
+-----+
1 row in set (0.00 sec)

UNION ALL example:

SELECT 'foo' AS bar UNION ALL SELECT 'foo' AS bar

Result:

+-----+
| bar |
+-----+
| foo |
| foo |
+-----+
2 rows in set (0.00 sec)

Having Machine Learning, Data Science or Python Interview? Check 👉 52 SQL Interview Questions

Q12: 
Delete duplicate values in a SQL table

Answer
Join MLStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 25k+ Data Scientists Who Trust MLStack.Cafe

Q13: 
Department Highest Salary Challenge

Answer
Join MLStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 25k+ Data Scientists Who Trust MLStack.Cafe

Q14: 
Explain the difference between Exclusive Lock and Update Lock

Answer
Join MLStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 25k+ Data Scientists Who Trust MLStack.Cafe

Q15: 
How can we transpose a table using SQL (changing rows to column or vice-versa)?

Answer
Join MLStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 25k+ Data Scientists Who Trust MLStack.Cafe

Q16: 
How does B-trees Index work?

Answer
Join MLStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 25k+ Data Scientists Who Trust MLStack.Cafe

Q17: 
How does TRUNCATE and DELETE operations effect Identity?

Senior 
Answer
Join MLStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 25k+ Data Scientists Who Trust MLStack.Cafe

Q18: 
What is the cost of having a database index?

Answer
Join MLStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 25k+ Data Scientists Who Trust MLStack.Cafe

Q19: 
What is the difference among UNION, MINUS and INTERSECT?

Answer
Join MLStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 25k+ Data Scientists Who Trust MLStack.Cafe

Q20: 
What would happen without an Index?

Answer
Join MLStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 25k+ Data Scientists Who Trust MLStack.Cafe

Q21: 
How does database Indexing work?

Answer
Unlock MLStack.Cafe to open all answers and get your next figure job offer!
 

Q22: 
Select first row in each GROUP BY group (greatest-n-per-group problem)?

Answer
Unlock MLStack.Cafe to open all answers and get your next figure job offer!
 

Q23: 
What is the difference between B-Tree, R-Tree and Hash indexing?

Answer
Unlock MLStack.Cafe to open all answers and get your next figure job offer!
 
 

Prepare for AI developer and engineer interviews with 19 answered OpenClaw questions covering Gateway architecture, channels, agent workspaces, memory, MCP, model failover, multi-agent routing, security, sandboxing, approvals, and remote operations....

Prepare for AI agent developer interviews with 15 Model Context Protocol (MCP) questions covering tools, resources, prompts, JSON-RPC, transports, roots, sampling, security, and practical MCP server design....

Amazone runs the internet as we know it. Amazon Web Services (AWS) offers a comprehensive suite of machine learning (ML) services that cater to various needs and expertise levels. Follow along and learn the 23 most common AWS machine-learning intervi...

Azure Machine Learning (Azure ML) is a cloud-based service for creating and managing machine learning solutions. It’s designed to scale, distribute, and deploy machine learning models to the cloud. Follow along and learn the 23 most common Azure Mach...
Hadoop is an open-source big data processing framework. It leverages distributed computing to store and process large datasets in a fault-tolerant manner. According to recent reports, Apache Hadoop is one of the most sought-after big data skills with...
Apache Spark is a unified analytics engine for large-scale data processing. It is built to handle various use cases in big data analytics, including data processing, machine learning, and graph processing. Follow along and learn the 23 most common an...
Scala is a powerful language with functional programming capabilities that can be a good choice for data science, especially in big data and distributed computing scenarios. As an example, Apache Spark, a popular distributed data processing framework...
PyTorch popularity as a Deep Learning framework of choice is on the rise. As of December 2022, 62% of the academic papers were implemented in PyTorch whereas only 4% were for TensorFlow. Follow along and prepare effectively with these key 30 PyTorch ...
The use of Artificial Intelligence (AI) in machine learning and data science enabled advancements in areas such as natural language processing, computer vision, recommendation systems, fraud detection, predictive analytics, and personalized medicine....
Optimization algorithms are extensively used in training machine learning models. Data engineers employ algorithms like gradient descent, stochastic gradient descent, and variants (e.g., Adam, RMSprop) to optimize the model parameters and minimize th...