140+ SQL Interview Questions and Answers (2022) - Great Learning
140+ SQL Interview Questions and Answers (2022) - Great Learning
SQL Interview
ALL COURSES Questions
FREE COURSES and Answers
INTERVIEW QUESTIONS CAREER in STORIES
VISUAL
2022
By Neha Kaplish - Jan 18, 2022
If you’re preparing for an upcoming SQL Interview, you’re at the right place! This blog on
SQL Interview Questions has been divided into various sections. Whether you’re a
fresher, a developer, or an experienced working professional, there are SQL Interview
Questions to meet your needs and help you ace your interview!
enables you to do just that. There is a high demand among employers for individuals
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
who possess SQL skills. It is therefore absolutely essential for you to prepare yourself with
the frequently asked SQL Interview questions if you want to land your dream job!
>
2 Years | Live online classes |
Management Systems Weekends Science a
Intelligen
6 months Course | Flexible Applications Close Today
Module Based Learning Path |
2 Years | On
Start learning at 750 USD | Classes | We
Weekly Mentored Sessions
Application
Applications Close Today
1. What is Database?
A database is a system that helps in collecting, storing and retrieving data. Databases
can be complex, and such databases are developed using design and modelling
approaches.
2. What is DBMS?
DBMS stands for Database Management System which is responsible for the creating,
updating, and managing of the database.
RDBMS stands for Relational Database Management System that stores data in the
form of a collection of tables, and relations can be defined between the common fields
of these tables.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 2/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
4.ALL
What is SQL? FREE COURSES
COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
SQL stands for Structured Query Language. It is the primary language to interact with
databases. With the help of SQL, we can extract data from a database, modify this data
and also update it whenever there is a requirement. This query language is evergreen
and is widely used across industries. For example, if a company has records of all the
details of their employees in the database. With the help of SQL, all of this data can be
queried to find out valuable insights in a short span of time.
column1 datatype,
column2 datatype,
column3 datatype,
....
);
We will start off by giving the keywords, CREATE TABLE, then we will give the name of the
table. After that in braces, we will list out all the columns along with their datatypes.
name varchar(25),
age int,
gender varchar(25),
....
);
The above command will completely delete all the data present in the table along with
the table itself.
But if we want to delete only the data present in the table but not the table itself, then
we will use the truncate command:
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 3/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
RENAME TO new_table_name;
We will start off by giving the keywords ALTER TABLE, then we will follow it up by giving the
original name of the table, after that, we will give in the keywords RENAME TO and finally,
we will give the new table name.
RENAME TO employee_information;
To be more thorough with your preparation, do check out our Free Course on SQL
Interview Questions at Great Learning Academy. In this course, we start off with
interview questions on the basics of SQL. Then, we dive into the questions of
Normalization and Sub-queries. Finally, we have interview questions on two projects
“Broadband management system” and “HR DBMS”. You can also take up the
software engineer certificate course offered by Great Learning in collaboration with
leading universities. Great Learning offers career assistance and dedicated mentor
support to help power ahead your career.
WHERE [condition];
We will start off by giving the keywords DELETE FROM, then we will give the name of the
table, after that we will give the WHERE clause and give the condition on the basis of
which we would want to delete a row.
For example, from the employee table, if we would like to delete all the rows, where the
age of the employee is equal to 25, then this will the command:
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 4/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
WHERE [age=25];
Generally, in a table, we will have a lot of redundant information which is not required,
so it is better to divide this complex table into multiple smaller tables which contains
only unique information.
It is in 2NF.
No transitive dependency exists between non-key attributes and key attributes through
another non-key attribute
Start
744
Types of Joins:
•LEFT JOIN − Returns all rows from the left table, even if there are no matches in the right
table.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 6/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
•RIGHT JOIN − Returns all rows from the right table, even if there are no matches in the
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
left table.
•FULL OUTER JOIN − Returns rows when there is a match in one of the tables.
•SELF JOIN − Used to join a table to itself as if the table were two tables, temporarily
renaming at least one table in the SQL statement.
•CARTESIAN JOIN (CROSS JOIN) − Returns the Cartesian product of the sets of records
from the two or more joined tables.
INNER JOIN:
The INNER JOIN creates a new result table by combining column values of two tables
(table1 and table2) based upon the join-predicate. The query compares each row of
table1 with each row of table2 to find all pairs of rows which satisfy the join-predicate.
SYNTAX :
FROM table1
ON table1.commonfield = table2.commonfield;
LEFT JOIN:
The LEFT JOIN returns all the values from the left table, plus matched values from the
right table or NULL in case of no matching join predicate.
SYNTAX :
FROM table1
ON table1.commonfield = table2.commonfield;
RIGHT JOIN:
The RIGHT JOIN returns all the values from the right table, plus matched values from the
left table or NULL in case of no matching join predicate.
SYNTAX :
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 7/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
SELECT
ALL table1.col1,
COURSES table2.col2,…,
FREE COURSES table1.coln
ON table1.commonfield = table2.commonfield;
The FULL OUTER JOIN combines the results of both left and right outer joins. The joined
table will contain all records from both the tables and fill in NULLs for missing matches
on either side.
SYNTAX :
FROM table1
ON table1.commonfield = table2.commonfield;
Union
FROM table1
ON table1.commonfield = table2.commonfield;
SELF JOIN:
The SELF JOIN joins a table to itself; temporarily renaming at least one table in the SQL
statement.
SYNTAX:
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 8/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
Hierarchical
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
Network
Relational
Object-Oriented.
Out of these database management systems, SQL Server comes under the category of
Relational database management system. A relational database refers to a database
that stores data in a structured format, using rows and columns. This makes it easier to
locate and access specific values within the database. It is “relational” because the
values within each table are related to each other. The relational structure makes it
possible to run queries across multiple tables at once.
“INSERT INTO tablename (col_name, col_date) VALUES (‘DATE: Manual Date’, ‘2020-
9-10’)”;
Let’s look at the different types of constraints which are present in SQL:
Constraint Description
NOT NULL Ensures that a column cannot have a NULL value.
DEFAULT Provides a default value for a column when none is specified.
UNIQUE Ensures that all the values in a column are different
PRIMARY Uniquely identifies each row/record in a database table
FOREIGN Uniquely identifies a row/record in any another database table
The CHECK constraint ensures that all values in a column satisfy certain
CHECK
conditions.
Used to create and retrieve data from the database very quickly.
INDEX
You can consider Primary Key constraint to be a combination of UNIQUE and NOT NULL
constraint. This means that if a column is set as a primary key, then this particular
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 9/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
column cannot have any null values present in it and also all the values present in this
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
column must be unique.
Show tables;
One thing to be noted over here is that pl sql is only for oracle databases. If you don’t
have an Oracle database, then you cant work with PL SQL.
While, with the help of sql, we were able to DDL and DML queries, with the help of PL SQL,
we will be able to create functions, triggers and other procedural constructs.
Hierarchical
Network
Relational
Object – Oriented.
Out of these database management systems, MYSQL comes under the category of
Relational database management system. A relational database refers to a database
that stores data in a structured format, using rows and columns. This makes it easier to
locate and access specific values within the database. It is “relational” because the
values within each table are related to each other. The relational structure makes it
possible to run queries across multiple tables at once.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 10/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
To see all the tables in MYSQL, we would have to use this query:
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
show tables;
SELECT
table_name
FROM
User_tables;
SELECT
FROM
Information_schema.tables;
ORACLE
MYSQL
SQL Server
Hence, to implement sql queries, we would need to install any of these Relational
Database Management Systems.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 11/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
UPDATE employees
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
SET last_name=‘Cohen’
WHERE employee_id=101;
With this update command, I am changing the last name of the employee.
Command Description
CREATE Create objects in the database
ALTER Alters the structure of the database object
DROP Delete objects from the database
TRUNCATE Remove all records from a table permanently
COMMENT Add comments to the data dictionary
RENAME Rename an object
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 12/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
23.
ALLWrite
COURSESSQL
query to fetch
FREE COURSES employee
INTERVIEW names
QUESTIONS having
CAREER a salary
VISUAL STORIES
greater than or equal to 20000 and less than or equal 10000.
By using BETWEEN in the where clause, we can retrieve the Employee Ids of employees
with salary >= 20000and <=10000. SELECT FullName FROM EmployeeDetails WHERE EmpId
IN (SELECT EmpId FROM EmployeeSalary WHERE Salary BETWEEN 5000 AND 10000)
29. Can you get the list of employees with same salary?
Select distinct e.empid,e.empname,e.salary from employee e, employee e1 where
e.salary =e1.salary and e.empid != e1.empid
1. ROWCOUNT function
Once we have a clear understanding of the schema, this helps in a lot of ways:
We can decide which user has access to which tables in the database.
We can modify or add new relationships between different entities in the database.
Overall, you can consider a schema to be a blueprint for the database, which will give
you the complete picture of how different objects interact with each other and which
users have access to different entities.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 14/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
Now, if you want to extract all the records “where” the value of “Sepal.Length” is greater
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
than 6, then you can use a query like this:
As, you see, we have extracted all the records, where the value of ‘Sepal.Length’ is
greater than 6.
Similarly, if you want to extract all records where ‘Species’ is Virginia, this will be the
query:
We will start off by giving the keywords ALTER TABLE, then we will give the name of the
table, following which we will give the keywords DROP COLUMN and finally give the name
of the column which we would want to remove.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 15/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
Unique Key:
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
Whenever we give the constraint of unique key to a column, this would mean that the
column cannot have any duplicate values present in it. In other words, all the records
which are present in this column have to be unique.
In the above command, we are giving two conditions. The condition ensures that we
extract only those records where the first name of the employee is ‘Steven’ and the
second condition ensures that the salary of the employee is less than $10,000. In other
words, we are extracting only those records, where the employee’s first name is ‘Steven’
and this person’s salary should be less than $10,000.
You can refer this SQL beginner’s tutorial if you want to know more about Structure
Query Language
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 16/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
begin
if( abs(:new.transaction_amount)>999999)THEN
end if;
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 17/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
end;
VALUES
...
We start off by giving the keywords INSERT INTO then we give the name of the table into
which we would want to insert the values. We will follow it up with the list of the columns,
for which we would have to add the values. Then we will give in the VALUES keyword and
finally, we will give the list of values.
name,
age,
salary)
VALUES
'Sam',
21,
75000
),
' 'Matt',
32,
85000 ),
'Bob',
26,
90000
);
In the above example, we are inserting multiple records into the table called employees.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 18/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
4.ALL
How to find
COURSES theCOURSES
FREE nth highest
salary
INTERVIEW in SQL?
QUESTIONS CAREER VISUAL STORIES
This is how we can find the nth highest salary in SQL SERVER using TOP keyword:
SELECT TOP 1 salary FROM ( SELECT DISTINCT TOP N salary FROM #Employee ORDER BY
salary DESC ) AS temp ORDER BY salary
This is how we can find the nth highest salary in MYSQL using LIMIT keyword:
This is how we can copy all the columns into a new table:
SELECT *
INTO newtable
FROM oldtable
WHERE condition;
INTO newtable
FROM oldtable
WHERE condition;
This command helps us to add a new column named as contact in the employees
table.
With this command, we will be able to extract all the records where the first name is like
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
“Steven”.
16. How can you create an empty table from an existing table?
Lets take an example: Select * into studentcopy from student where 1=2 Here, we are
copying student table to another table with the same structure with no rows copied.
2. Clustered indexes store data physically in the table or view whereas, non-clustered
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
indexes do not store data in the table as it has separate structure from the data row.
1. Safe Access Sandbox: Here a user can perform SQL operations such as creating stored
procedures, triggers etc. but cannot have access to the memory as well as cannot
create files.
2. External Access Sandbox: Users can access files without having the right to
manipulate the memory allocation.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 22/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
3. Unsafe Access Sandbox: This contains untrusted codes where a user can have access
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
to memory.
SELECT
x,
y,
COUNT(*) occurrences
FROM z1
GROUP BY
x,
HAVING
COUNT(*) > 1;
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 23/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
2.ALL
What is Case
COURSES FREEWHEN
COURSESin
SQL?
INTERVIEW QUESTIONS CAREER VISUAL STORIES
If you have knowledge about other programming languages, then you’d have learnt
about if-else statements. You can consider Case WHEN to be analogous to that.
In Case WHEN, there will be multiple conditions and we will choose something on the
basis of these conditions.
CASE
ELSE result
END;
We start off by giving the CASE keyword, then we follow it up by giving multiple WHEN,
THEN statements.
FROM employees
FROM employees);
delete e
from emp e
inner join
(select *,
where e.Rank>1
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 24/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
Below is the syntax to delete duplicate records using groupby and min:
left join
Implicit Cursor
Explicit Cursor
Implicit Cursor:
These implicit cursors are default cursors which are automatically created. A user
cannot create an implicit cursor.
Explicit Cursor:
Explicit cursors are user-defined cursors. This is the syntax to create explicit cursor:
We start off by giving by keyword DECLARE, then we give the name of the cursor, after
that we give the keywords CURSOR FOR SELECT * FROM, finally, we give in the name of the
table.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 25/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
other languages. This means that we can store a SQL statement as a stored procedure
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
and this stored procedure can be invoked whenever we want.
AS
sql_statement
GO;
We start off by giving the keywords CREATE PROCEDURE, then we go ahead and give the
name of this stored procedure. After that, we give the AS keyword and follow it up with
the SQL query, which we want as a stored procedure. Finally, we give the GO keyword.
EXEC procedure_name;
We will give in the keyword EXEC and then give the name of the stored procedure.
AS
GO;
In the above command, we are creating a stored procedure which will help us to
extract all the employees who belong to a particular location.
With this, we are extracting all the employees who belong to Boston.
We start off by giving the keywords CREATE INDEX and then we will follow it up with the
name of the index, after that we will give the ON keyword. Then, we will give the name of
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 26/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
the table on which we would want to create this index. Finally, in parenthesis, we will list
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
out all the columns which will have the index. Let’s look at an example:
ON Employees (Salary);
In the above example, we are creating an index called a salary on top of the ‘Salary’
column of the ‘Employees’ table.
We start off with the keywords CREATE UNIQUE INDEX, then give in the name of the index,
after that, we will give the ON keyword and follow it up with the name of the table.
Finally, in parenthesis, we will give the list of the columns which on which we would want
this unique index.
ALTER
ALL TABLEtable_name
We start off by giving the keywords ALTER TABLE, then we will give in the name of the
table. After that, we will give in the keywords MODIFY COLUMN. Going ahead, we will give
in the name of the column for which we would want to change the datatype and finally
we will give in the data type to which we would want to change.
IN MYSQL, we will start off by using the ALTER TABLE keywords, then we will give in the
name of the table. After that, we will use the CHANGE keyword and give in the original
name of the column, following which we will give the name to which we would want to
rename our column.
In ORACLE, we will start off by using the ALTER TABLE keywords, then we will give in the
name of the table. After that, we will use the RENAME COLUMN keywords and give in the
original name of the column, following which we will give the TO keyword and finally
give the name to which we would like to rename our column.
When it comes to SQL Server, it is not possible to rename the column with the help of
ALTER TABLE command, we would have to use sp_rename.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 28/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
2.ALL
What is a view
COURSES in SQL?
FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
A view is a database object that is created using a Select Query with complex logic, so
views are said to be a logical representation of the physical data, i.e Views behave like a
physical table and users can use them as database objects in any part of SQL queries.
Simple View
Complex View
Inline View
Materialized View
Simple View:
Simple views are created with a select query written using a single table. Below is the
command to create a simple view:
Complex View:
Inline View:
A subquery is also called as an inline view if and only if it is called in FROM clause of a
SELECT query.
We will start off by giving the keywords ALTER TABLE, then we will give the name of the
table, following which we will give the keywords DROP COLUMN and finally give the name
of the column which we would want to remove.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 29/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
4.ALL
How to remove
COURSES duplicate
FREE COURSES rows QUESTIONS
INTERVIEW in SQL? CAREER VISUAL STORIES
There are a lot of ways to remove duplicate rows in SQL. Let’s look at this example:
SELECT [Name],
[Age],
[Gender],
COUNT(*) AS CNT
FROM [mydata].[dbo].[Employees]
GROUP BY [Name],
[Age],
[Gender]
In the above command, we are using group by and having to count the duplicate
records.
Types of Joins:
•LEFT JOIN − Returns all rows from the left table, even if there are no matches in the right
table.
•RIGHT JOIN − Returns all rows from the right table, even if there are no matches in the
left table.
•FULL OUTER JOIN − Returns rows when there is a match in one of the tables.
•SELF JOIN − Used to join a table to itself as if the table were two tables, temporarily
renaming at least one table in the SQL statement.
•CARTESIAN JOIN (CROSS JOIN) − Returns the Cartesian product of the sets of records
from the two or more joined tables.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 30/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
With this command, we will be able to extract all the records where the salary of the
employee is between 10000 and 20000.
b. DML (Data Manipulation language): Used to manipulate already existing data in the
database, commands like SELECT, UPDATE, INSERT
c. DCL (Data Control Language): Used to control access to data in the database,
commands like GRANT, REVOKE
The first parameter is checked for a NULL value, if it is NULL then the 2nd parameter is
returned, otherwise, it returns the first parameter.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 32/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
SQL Constraints are used to specify the rules of data type in a table. They can be
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
specified while creating and altering the table. The following are the constraints in SQL:
NOT NULL CHECK DEFAULT UNIQUE PRIMARY KEY FOREIGN KEY
By using EXEC
By using sp_executesql
20. What are the main differences between #temp tables and
@table variables and which one is preferred ?
1. SQL server can create column statistics on #temp tables.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 33/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
22.
ALLWhat
COURSESis
recursive stored
FREE COURSES procedure?
INTERVIEW QUESTIONS CAREER VISUAL STORIES
A stored procedure which calls by itself until it reaches some boundary condition. This
recursive function or procedure helps programmers to use the same set of code any
number of times.
A query first takes the lowest level lock possible with the smallest row-level.When too
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
many rows are locked, the lock is escalated to a range or page lock. If too many pages
are locked, it may escalate to a table lock.
Exists: Works on Virtual tables Is used with co-related queries Exits comparison when
match is found
34. How do you copy data from one table to another table ?
INSERT INTO table2 (column1, column2, column3, …) SELECT column1, column2, column3, …
FROM table1 WHERE condition;
35. List the ACID properties that makes sure that the database
transactions are processed
ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties that guarantee
that database transactions are processed reliably.
This query will return 10 records as TRUNCATE was executed in the transaction. TRUNCATE
does not itself keep a log but BEGIN TRANSACTION keeps track of the TRUNCATE
command.
and can execute them wherever required by applying some conditional logic to it.
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
Stored procedures are also used to reduce network traffic and improve performance.
1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 36/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
45.
ALL How would
COURSES you
FREE select
COURSES all the users,
INTERVIEW whose
QUESTIONS phone
CAREER number
VISUAL STORIES
is NULL?
SELECT user_name FROM users WHERE ISNULL(user_phonenumber);
47. Write an SQL query to show the second highest salary from
a table.
Select max(Salary) from Worker where Salary not in (Selct max(Salary) from Worker);
1. TINYTEXT
2. TEXT
3. MEDIUMTEXT
4. LONGTEXT
The group by clause combines all those records that have identical values in a
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
particular field or any group of fields.
53. How do you get the last id without the max function?
SELECT ID from table order by ID desc limit 1
54. Write a SQL query to fetch only even rows from the table.
Using the same Row_Number() and checking that the remainder when divided by 2 is
0- SELECT E.EmpId, E.Project, E.Salary FROM ( SELECT *, Row_Number() OVER(ORDER BY
EmpId) AS RowNumber FROM EmployeeSalary ) E WHERE E.RowNumber % 2 = 0
55. Write a SQL query to create a new table with data and
structure copied from another table.
Using SELECT INTO command- SELECT * INTO newTable FROM EmployeeDetails;
Select * from Employee where salary >= 25000 and salary <= 50000;
The following query will not fetch a record with the salary of 6000 but also will skip the
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
record with NULL.
In SQL you can write In PL/SQL you can write block of code
Writes queries and command that has procedures, functions,
using DDL, DML statements. packages or variables, etc.
Using SQL, you can retrieve, Using PL/SQL, you can create
modify, add, delete, or applications or server pages that
Use
manipulate the data in the display the information obtained from
database. SQL in a proper format.
3 The WHERE clause cannot contain The HAVING clause can contain
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
aggregate functions aggregate functions
Oracle,
MySQL, Document: MongoDB and CouchDB, Key-value: Redis
Examples Microsoft SQL and DynamoDB, Wide-column: Cassandra and HBase,
Server, and Graph: Neo4j and Amazon Neptune
PostgreSQL
Document: general purpose, Key-value: large
amounts of data with simple lookup queries, Wide-
Primary General
column: large amounts of data with predictable
Purpose purpose
query patterns, Graph: analyzing and traversing
relationships between connected data
Schemas Rigid Flexible
Vertical
(scale-up
Scaling Horizontal (scale-out across commodity servers)
with a larger
server)
Multi-Record Supported Most do not support multi-record ACID transactions.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 40/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
Typically
Joins Typically not required
required
Requires ORM
Data to Many do not require ORMs. MongoDB documents
(object-
Object map directly to data structures in most popular
relational
Mapping programming languages.
mapping)
Helps to Process and Analyze the Data Using Simple Helps to Add Business
Queries Logic into an Application
System System
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
MongoDB vs SQL
MongoDB MySQL
When you need high availability If you’re just starting and your database is not
of data with automatic, fast, and going to scale much, MYSQL will help you in easy
instant data recovery and low-maintenance setup
SQL Server is widely used for data analysis and also scaling up of data. SQL Server can
be used in conjunction with Big Data tools such as Hadoop.
SQL Server can be used to process data from various data sources such as Excel, Table,
.Net Framework application, etc.
1. Click on the below SQL Server official release link to access the latest version:
https://www.microsoft.com/en-in/sql-server/sql-server-downloads
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 42/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
2.Select the type of SQL Server edition that you want to install. SQL Server can be used on
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
a Cloud Platform or as an open-source edition(Express or Developer) in your local
computer system.
4.Save the .exe file on your system. Right-click on the .exe file and click on Open.
5.Click on ‘Yes’ to allow the changes to be made on your system and have SQL Server
Installed.
6.Once the installation is complete, restart your system, if required, and launch the SQL
Server Management Studio application from the START menu.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 43/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
2. Click on the search icon and type in – SQL Server 2008 download
3. Click on the result link to download and save SQL Server 2008.
4. Select the type of the SQL Server edition that you want to install. SQL Server can be
used on a Cloud Platform or as an open-source edition(Express or Developer) in your
local computer system.
6. Save the .exe file on your system. Right-click on the .exe file and click on Open.
7. Click on ‘Yes’ to allow the changes to be made on your system and have SQL Server
installed.
8. Once the installation is complete, restart your system, if required, and launch the SQL
Server Management Studio application.
2. Click on the search icon and type in – SQL Server 2017 download
3. Click on the result link to download and save SQL Server 2017.
4. Select the type of the SQL Server edition that you want to install. SQL Server can be
used on a Cloud Platform or as an open-source edition(Express or Developer) in your
local computer system.
6. Save the .exe file on your system. Right-click on the .exe file and click on Open.
7. Click on ‘Yes’ to allow the changes to be made on your system and have SQL Server
installed.
8. Once the installation is complete, restart your system, if required, and launch the SQL
Server Management Studio application from the START menu.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 44/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
2. Click on the search icon and type in – SQL Server 2014 download
3. Click on the result link to download and save SQL Server 2014.
4. Select the type of SQL Server edition that you want to install. SQL Server can be used
on a Cloud Platform or as an open-source edition(Express or Developer) in your local
computer system.
6. Save the .exe file on your system. Right-click on the .exe file and click on Open.
7. Click on ‘Yes’ to allow the changes to be made on your system and have SQL Server
Installed.
8. Once the installation is complete, restart your system, if required, and launch the SQL
Server Management Studio application from the START menu.
2. Click on the search icon and type in – SQL Server 2012 download
3. Click on the result link to download and save SQL Server 2012.
4. Select the type of the SQL Server edition that you want to install. SQL Server can be
used on a Cloud Platform or as an open-source edition(Express or Developer) in your
local computer system.
6. Save the .exe file on your system. Right-click on the .exe file and click on Open.
7. Click on ‘Yes’ to allow the changes to be made on your system and have SQL Server
Installed.
8. Once the installation is complete, restart your system, if required, and launch the SQL
Server Management Studio application from the START menu.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 45/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
10.
ALLWhat
COURSESis cte inCOURSES
FREE SQL Server?
INTERVIEW QUESTIONS CAREER VISUAL STORIES
CTEs are Common Table Expressions that are used to create temporary result tables
from which data can be retrieved/ used. The standard syntax for a CTE with a SELECT
statement is:
FROM EMPLOYEE)
(select distinct salary, dense_rank() over (order by salary desc) as salary rank from
employees)
(select distinct salary, dense_rank() over (order by salary desc) as salaryrank from
employees)
In this way, CTEs can be used to find the nth highest salary within an organisation.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 46/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
Click on Logins and open your database connection. Type in the new password for login
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
and click on ‘OK’ to apply the changes.
FROM EMPLOYEE
GROUP BY COL1
SELECT @@version;
You can execute the Stored Proc by using the command Exec Procedure_Name;
2. In the Object Explorer window pane, right-click on Databases and select ‘New
Database’
Syntax:
ON TABLE_NAME(COL1, COL2);
Rows and Columns. The below syntax can be used to create a new table with 3
columns.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 48/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
COLUMN1 DATATYPE,
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
COLUMN2 DATATYPE,
COLUMN3 DATATYPE
);
Alternatively, you can right-click on Table in the Object Explorer window pane and select
‘New -> Table’.
You can also define the type of Primary/ Foreign/ Check constraint when creating a
table.
2. In the dialog box shown below, select the Server Type as Database Engine and Server
Name as the name of your laptop/ desktop system.
3. Select the appropriate Authentication type and click on the Connect button.
4. A secure connection would be established, and the list of the available Databases
will be loaded in the Object Explorer window pane.
FROM EMPLOYEE
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 49/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
GROUP BY COL1
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
https://www.microsoft.com/en-in/sql-server/sql-server-downloads
2. In the dialog box shown below, select the Server Type as Database Engine and Server
Name as the name of your laptop/ desktop system and click on the Connect button.
4. A secure connection would be established, and the list of the available Databases
will be loaded in the Object Explorer window pane.
3. Click on the search icon and type in – SQL Server 2014 download
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
4. Click on the result link to download and save SQL Server 2014.
Syntax:
1 CASE
2 WHEN CONDITION1 THEN RESULT1
3
4 WHEN CONDITION2 THEN RESULT2
5
6 ELSE
7 RESULT
8 END;
Sample query:
select
sum (
case
else
end ) as Canada
from company_regions;
SELECT
SUM (
CASE
ELSE
END
) AS “Mass”,
SUM (
CASE
ELSE
END
) AS “Economic”,
SUM (
CASE
ELSE
END
) AS ” Luxury”
FROM
film;
Go to the routed website and click on the link to download. Once the download is
complete, open the .exe file to install the content of the file. Once the installation is
complete, refresh or restart the system, as required.
Alternatively, once SQL Server is installed and launched, it will prompt the user with an
option to launch SQ Server Management Studio.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 52/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
31.ALLHow to write
COURSES aCOURSES
FREE storedprocedure in SQL Server
INTERVIEW QUESTIONS CAREER VISUAL STORIES
A Stored Procedure is nothing but a frequently used SQL query. Queries such as a SELECT
query, which would often be used to retrieve a set of information many times within a
database, can be saved as a Stored Procedure. The Stored Procedure, when called,
executes the SQL query save within the Stored Procedure.
You can execute the Stored Proc by using the command Exec Procedure_Name;
For example: Trigger to update the employee’s salary to $40000 AFTER getting a
promotion to job level band D1. It is also referred to as a type of Stored Procedure. The
type of trigger as AFTER or BEFORE is defined in the syntax of the trigger itself.
Make sure to select the Server Name as the desktop or laptop system name.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 53/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
Click on the START menu and select All Programs. Select Microsoft SQL Server, select
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
Configuration Tools, and then select SQL Server Configuration tools. In that, select the
SQL Server Configuration Manager.
Some of the common actions that can be taken on the data are CREATE, DELETE, INSERT,
UPDATE, SELECT, REVOKE, etc.
SQL Server can also be used to import and export data from different data sources. SQL
Server can also be connected to various other databases/ .Net framework using
Connection Strings.
SQL Server can also be used in conjunction with Big Data tools like Hadoop.
AGGREGATE FUNCTIONS:
COUNT()
STRING FUNCTIONS:
COALESCE()
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 54/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
CAST()
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
CONCAT()
SUBSTRING()
DATE FUNCTIONS:
GETDATE()
DATEADD()
DATEDIFF()
There are many types of functions such as Aggregate Functions, Date Functions, String
Functions, Mathematical functions, etc.
40. How to find nth highest salary in SQL Server without using
a subquery
Query to find the 10 highest salary. For up-gradation of the b10 band.
(select distinct salary, dense_rank() over (order by salary desc) as salaryrank from
employees)
(select distinct salary, dense_rank() over (order by salary desc) as salaryrank from
employees)
In this way, by replacing the salaryrank value, we can find the nth highest salary in any
organisation.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 55/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
2. Click on the search icon and type in – SQL Server 2012 download
3. Click on the result link to download and save SQL Server 2012.
4. Select the type of the SQL Server edition that you want to install. SQL Server can be
used on a Cloud Platform or as an open-source edition(Express or Developer) in your
local computer system.
6. Save the .exe file on your system. Right-click on the .exe file and click on Open.
7. Click on ‘Yes’ to allow the changes to be made on your system and have SQL Server
Installed
Syntax:
Temporary tables do not occupy any physical memory and can be used to retrieve
data faster.
There are different types of schema. Some of the most popular ones are Star schema
and Snowflake schema. Star schema is the one that has the entities represented in a
star shape, and snowflake schema has its entities represented in a snowflake shape.
This brings us to the end of the SQL Interview questions. Glad to see you are now better
equipped to face an interview.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 56/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
There are many sources online that can help you prepare for an SQL interview. You can
go through brief tutorials and free online courses on SQL (eg.: SQL basics on Great
Learning Academy) to revise your knowledge of SQL. You can also practice projects to
help you with practical aspects of the language. Lastly, there are many blogs such as
this that list out all the probable questions that an interviewer might ask.
SQL is a vast topic and there is a lot to learn. But the most basic skills that an SQL
professional should know are:
Managing a database
There are some platforms available online that can help you practice SQL such as SQL
Fiddle, SQLZOO, W3resource, Oracle LiveSQL, DB-Fiddle, Coding Groud, GitHub and
others.
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 57/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
There are some platforms available online that can help you practice SQL such as SQL
Fiddle, SQLZOO, W3resource, Oracle LiveSQL, DB-Fiddle, Coding Groud, GitHub and
others.
You can also refer to articles and blogs online that list the most important SQL interview
questions for preparation.
CREATE DATABASE
ALTER DATABASE
CREATE TABLE
ALTER TABLE
DROP TABLE
CREATE INDEX
DROP INDEX
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 58/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
CREATE DATABASE
ALL COURSES FREE COURSES INTERVIEW QUESTIONS CAREER VISUAL STORIES
ALTER DATABASE
CREATE TABLE
ALTER TABLE
DROP TABLE
CREATE INDEX
DROP INDEX
9. Is SQL coding?
Yes, SQL is a coding language/ programming language that falls under the category of
domain-specific programming language. It is used to access relational databases such
as MySQL.
SQL helps you update, delete, and request information from databases. Some of the
examples of SQL are in the form of the following statements:
SELECT
INSERT
UPDATE
DELETE
CREATE DATABASE
ALTER DATABASE
SQL code is used to access and communicate with a database. It helps in performing
tasks such as updating and retrieving data from the databases.
Wondering where to learn the highly coveted in demand skills for free? Check out the
courses on Great Learning Academy. Enroll to any course, learn the in demand skill and
get your free certificate. Hurry!
27
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 59/60
3/14/22, 1:44 PM 140+ SQL Interview Questions and Answers [2022]- Great Learning
https://www.mygreatlearning.com/blog/sql-interview-questions/#experienced 60/60