80 SQL Interview Question
80 SQL Interview Question
Whether you're a job hunter who is looking for a new opportunity to apply your SQL
skills or a hiring manager who is going to interrogate a candidate for a job opening in
their company, knowing common SQL interview questions and answers to them is a
must for you.
In this article, we're going to take a look at 80 essential SQL questions and answers
for beginners and intermediate practitioners that will help you better prepare for the
interview and know what to expect from your interviewer/interviewee.
Note that for the sake of convenience, this article is mostly addressing job searchers
since they are the main audience for such information. However, this content will
definitely be of use also for hiring managers/recruiters, especially for conducting their
first SQL interviews.
While this information can be mentioned in your resume, be ready to talk about it.
Naturally, there are no "right" answers to such questions, and there is no need to
Also, it's perfectly fine if you have only worked with one SQL flavor. Remember that
all SQL dialects are fairly similar among themselves. Therefore, being familiar with
only one of them is a solid basis for you to learn any others.
1. What is SQL?
It stands for Structured Query Language. A programming language used for
interaction with relational database management systems (RDBMS). This includes
fetching, updating, inserting, and removing data from tables.
retrieve and summarize the necessary information from a table or several tables
All in all, SQL allows querying a database in multiple ways. In addition, SQL easily
integrates with other programming languages, such as Python or R, so we can use
Data Control Language (DCL) – to control user access to the data in the
database and give or revoke privileges to a specific user or a group of users.
DQL: – SELECT
7. What is a database?
A structured storage space where the data is kept in many tables and organized so
that the necessary information can be easily fetched, manipulated, and summarized.
PRIMARY KEY – allows only unique and strictly non-null values ( NOT
NULL and UNIQUE ).
FOREIGN KEY – provides shared keys between two and more tables.
LEFT (OUTER) JOIN – returns all records from the left table and those records from
the right table that satisfy a defined join condition.
RIGHT (OUTER) JOIN – returns all records from the right table and those records
from the left table that satisfy a defined join condition.
FULL (OUTER) JOIN– returns all records from both (or all) tables. It can be
considered as a combination of left and right joins.
Clustered index – defines the physical order of records of a database table and
performs data searching based on the key values. A table can have only one
clustered index.
Non-clustered index – keeps the order of the table records that doesn't match
the physical order of the actual data on the disk. It means that the data is stored
in one place and a non-clustered index – in another one. A table can have
multiple non-clustered indexes.
Compound ( += , = , = , /= , etc.)
String ( % , _ , + , ^ , etc.)
UPDATE table_name
SET col_1 = value_1, column_2 = value_2
WHERE condition;
We can specify that we need a descending order using the DESC keyword; otherwise,
the order will be ascending by default. Also, we can sort by more than one column
and specify for each one, ascending or descending order separately. For example:
table_name; .
37. What is the DISTINCT statement and how do you use it?
This statement is used with the SELECT statement to filter out duplicates and return
only unique values from a column of a table. The syntax is:
Scalar functions – work on each individual value and return a single value.
On the other hand, SQL functions can be built-in (defined by the system) or user-
defined (created by the user for their specific needs).
COUNT() – returns the number of rows, including those with null values
UCASE() (in other SQL flavors – UPPER() ) – returns a string converted to the
upper case
INITCAP( ) – returns a string converted to the title case (i.e., each word of the
string starts from a capital letter)
MID() (in other SQL flavors – SUBSTR() ) – extracts a substring from a string
UCASE() (in other SQL flavors – UPPER( )) – returns a string converted to the
upper case
LCASE() (in other SQL flavors – LOWER() ) – returns a string converted to the lower
case
INITCAP() – returns a string converted to the title case (i.e., each word of the
string starts from a capital letter)
CONCAT() – joins two or more string values appending the second string to the
end of the first one
SUBSTR() – returns a part of a string satisfying the provided start and end points
LENGTH() (in other SQL flavors – LEN() ) – returns the length of a string, including
the blank spaces
LPAD() and RPAD() – return the padding of the left-side/right-side character for
right-justified/left-justified value
UNION ALL – returns the records obtained by at least one of two queries (including
duplicates)
EXCEPT (called MINUS in MySQL and Oracle) – returns only the records obtained
by the first query but not the second one
CASE
WHEN condition_1 THEN value_1
WHEN condition_2 THEN value_2
WHEN condition_3 THEN value_3
...
ELSE value
END;
In this way, we can also delete multiple records if they satisfy the provided condition.
To select all odd records, the syntax is identical in both cases, only that we would
use the inequality operator <> instead of = .
38. How to find the values in a text column of a table that start
with a certain letter?
Using the LIKE operator in combination with the % and _ wildcards. For example,
we need to find all surnames in a table that start with "A". The query is:
Here, we assume that a surname must contain at least two letters. Without this
assumption (meaning that a surname can be just A), the query is as follows:
SELECT id
FROM table_name
ORDER BY id DESC
LIMIT 1;
SELECT TOP 1 id
FROM table_name
ORDER BY id DESC
Key Takeaways
To sum up, we discussed the 80 essential beginner and intermediate SQL interview
questions and the right answers to them. Hopefully, this information will help you to
get ready for the interview and feel more confident, whether you're looking for a job
in SQL or hiring candidates for an intermediate SQL position.
https://instagram.com/bitsofds
https://bitsofds.quora.com/