SQL Interview Questions

Sapna Verma
3 min readAug 25, 2022

When I started preparing for data science interviews , I thought SQL is not that much important and it can be skipped which was my biggest mistake. I have gone through a few of interviews and thought to share the questions that were asked to me. May be it can helpful for all those who are preparing for data science interviews.

1. What is Primary Key and Foreign key ?

  1. Primary Key — It identifies each row uniquley. There can be only one primary key in one table. It can not be NULL.
  2. Foreign Key — There can be more than one foreign key. It is the primary key of another table. It is used to link two tables.

2. Difference between char and varchar ?

Char — It stores the character of fixed length. For example if we have used name char(5). And we use name “Mahi”. It will show length 5 only not 4. If the length of string is less than set or fixed length then it is padded with extra blank spaces so that its length became equal to the set length.

VarChar — It is a datatype in SQL which is used to store character string of variable length but maximum of set length specified. If the length of string is less than set or fixed length then it will store as it is without padded with extra blank spaces.

3. What is difference between where and having clause?

Where clause can not be used with aggregate functions but having clause is used with aggregate functions. where clause implements in row operations but having clause is implements in column operations. where clause is used with select , update and delete statements but having clause is only used with select statement.

4. What are Indexes in SQL ?

Indexes are used to create indexes in table.
Indexes are used to reterive the data from data bases more quickly.
The user can not see the indexes and they are just use to speed up the queries.
Note : Updating the table with indexes takes a lot of time than updating a table without indexes. It is because indexes also need an update.

5. How to get current year ?

SELECT YEAR(NOW()) AS CURRENT_YEAR;

6. Write a query to find the students name from a student table who have scored more than average marks ?

(SELECT NAME FROM STUDENTS WHERE MARKS >( SELECT AVG(MARKS) FROM STUDENTS));

we can not use where clause with aggregate functions so we have to use subquery here.

7. How to find out the cummulative sum of a numerical column ?

Suppose we have students table and we want to find out the cummulative sum for marks column. For this we can use over and order by clause.

select * , sum(marks) over (order by id) as running_total from students;

8.Write a SQL Query to find duplicate rows from table?

SELECT *, COUNT(empid) from employee group by empid having count(empid)>1;

9. How to find the max element form different numerical columns?

SELECT greatest(MAX(Marks1),MAX(Maeks2),MAX(Marks3)) AS MAX_MARKS From Marks;

10. HOW TO FIND THIRD HIGHEST SALARY from a table?

SELECT SALARY FROM EMPLOYEE ORDER BY SALARY DESC LIMIT 2,1;

Apart from this , questions were also asked from SQL joins that is also a really important topic to cover . you can try to solve HackerRank SQL queries that will also help to get a clear picture.

Conclusion :

That’s all for this article. I have collected the questions of SQL that I have faced in my few of the interviews. These interview questions can be revised if you are preparing for the role of machine learning engineer, data scientist, or any data science-related job.

Thanks for reading !

--

--