0% found this document useful (0 votes)
18 views

SQL For Data Analyst Part - 3

The document discusses 5 commonly asked SQL interview questions including the differences between primary key, unique key and foreign key; DISTINCT and GROUP BY; RANK, DENSE_RANK and ROW_NUMBER window functions; and using aggregate functions as window functions.

Uploaded by

chouayasaif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

SQL For Data Analyst Part - 3

The document discusses 5 commonly asked SQL interview questions including the differences between primary key, unique key and foreign key; DISTINCT and GROUP BY; RANK, DENSE_RANK and ROW_NUMBER window functions; and using aggregate functions as window functions.

Uploaded by

chouayasaif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

What is the difference between primary key,

unique key and foreign key

What is the difference between DISTINCT


and GROUP BY?

What are aggregate functions? Name and


explain different types of aggregate
functions in SQL?

What is the difference between RANK,


DENSE_RANK and ROW_NUMBER
window function?

Can we use aggregate function as window


function? If yes then how do we do it?
SQL INTERVIEW
QUESTIONS PART-3
PREM MANDAL
Data Analyst interviews can
be challenging, but with the
right preparation, you can
excel!

Here are 5 commonly asked


Data Analyst interview
questions on SQL along with
answers to help you succeed!
💼💡
What is the difference between primary key, unique key, and foreign key?

🔑 Primary Key is used to uniquely identify each row in a table and ensures that no two rows
have the same value. It helps in maintaining data integrity and forms the basis for
relationships with other tables.

🔑 Unique Key also ensures that each row has a unique value, but unlike the primary key, it
allows one row to have a NULL value. Tables can have multiple unique keys, but only one
primary key.

🔑 Foreign Key establishes a relationship between two tables by linking a column in one table
to the primary key of another table. It helps maintain referential integrity and ensures data
consistency.
What is the difference between DISTINCT and GROUP BY?

DISTINCT clause will return unique column values. Depending on the list of columns you provide , it will fetch the unique combination of values for
all those combined columns. If you provide just a single column in DISTINCT then it fetches just the unique values in that specific column.

Example below:
Below query returns unique employee names from the employee table:

SELECT DISTINCT name FROM employee;

Whereas below query would return unique combination of values based on all the columns from the employee table.

SELECT DISTINCT * FROM employee;

GROUP BY clause will group together the data based on the columns specified in group by. GROUP BY can also be used to fetch unique records from
a table but this is not why group by clause is used for. The main purpose of group by clause is to perform some aggregation (using the aggregate
functions like MIN, MAX, COUNT, SUM, AVG) based on the grouped by column values. Example below:

Below query would group together the data from employee table based on name column and then for each name value, it would count how many
records have the same name.

SELECT name, COUNT(1) FROM employee GROUP BY name;


What are aggregate functions? Name and explain different types of
aggregate functions in SQL?

Aggregate functions perform calculations on a set of values and return a single


value as a result. They are used with GROUP BY or without it to summarize
data.

Types of Aggregate Functions:

COUNT: Counts the number of rows in a group.


SUM: Calculates the sum of values in a group.
AVG: Calculates the average of values in a group.
MIN: Returns the minimum value in a group.
MAX: Returns the maximum value in a group.
What is the difference between RANK, DENSE_RANK and ROW_NUMBER window function?

RANK() function will assign a rank to each row within each partitioned result set. If multiple
rows have the same value then each of these rows will share the same rank. However the
rank of the following (next) rows will get skipped. Meaning for each duplicate row, one rank
value gets skipped.

DENSE_RANK() function will assign a rank to each row within each partitioned result set. If
multiple rows have the same value then each of these rows will share the same rank.
However the dense_rank of the following (next) rows will NOT get skipped.

This is the only difference between rank and dense_rank. RANK() function skips a rank if
there are duplicate rows whereas DENSE_RANK() function will never skip a rank.

ROW_NUMBER() function will assign a unique row number to every row within each
partitioned result set. It does not matter if the rows are duplicate or not.
Can we use aggregate function as window function? If yes then
how do we do it?

Yes, we can use aggregate function as a window function by using the OVER
clause. Aggregate function will reduce the number of rows or records since
they perform calculation of a set of row values to return a single value.

Whereas window function does not reduce the number of records.

SELECT SUM(Salary) OVER(PARTITION BY Department) AS Total_Salary


FROM Employees;
@PREMMANDAL
BUSINESS & DATA ANALYST

Was this helpful


WOULD YOU MIND SHOWING YOUR SUPPORT
BY GIVING IT A LIKE?

You might also like