180+ SQL Interview Questions (2023) - Great Learning
180+ SQL Interview Questions (2023) - Great Learning
BLOG Courses
IT/Software Development
Table of contents
Are you an aspiring SQL Developer? A career in SQL has seen an upward trend in 2023, and
you can be a part of the ever-so-growing community. So, if you are ready to indulge
yourself in the pool of knowledge and be prepared for the upcoming SQL interview, then
you are at the right place.
We have compiled a comprehensive list of SQL Interview Questions and Answers that will
come in handy at the time of need. Once you are prepared with the questions we
mentioned in our list, you will be ready to get into numerous SQL-worthy job roles like SQL
Developer, Business Analyst, BI Reporting Engineer, Data Scientist, Software Engineer,
Database Administrator, Quality Assurance Tester, BI Solution Architect and more.
Great Learning has prepared a list of the top 10 SQL interview questions that will help you
during your interview.
What is SQL?
What is a Database?
What is DBMS?
How to create a table in SQL?
How to delete a table in SQL?
How to change a table name in SQL?
How to create a database in SQL?
What is join in SQL?
What is Normalization in SQL?
How to insert a date in SQL?
https://www.mygreatlearning.com/blog/sql-interview-questions/ 1/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
What is SQL?
The acronym SQL stands for Structured Query Language. It is the typical language used for
relational database maintenance and a wide range of data processing tasks. The first SQL
database was created in 1970. It is a database language used for operations such as
database creation, deletion, retrieval, and row modification. It is occasionally pronounced
“sequel.” It can also be used to manage structured data, which is made up of variables
called entities and relationships between those entities.
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.
What is DBMS?
DBMS stands for Database Management System which is responsible for the creating,
updating, and managing of the database.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 2/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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.
We will start off by giving the keywords, CREATE TABLE, and then we will give the name of
the table. After that in braces, we will list out all the columns along with their data types.
There are two ways to delete a table from SQL: DROP and TRUNCATE. The DROP TABLE
command is used to completely delete the table from the database. This is the command:
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/ 3/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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.
We will be using the DELETE query to delete existing rows from the table:
We will start off by giving the keywords DELETE FROM, then we will give the name of the
table, and 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 be the command:
https://www.mygreatlearning.com/blog/sql-interview-questions/ 4/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Normalization is used to decompose a larger, complex table into simple and smaller ones.
This helps us in removing all the redundant data.
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 contain only unique
information.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 5/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
It is in 2NF.
No transitive dependency exists between non-key attributes and key attributes through
another non-key attribute
Joins are used to combine rows from two or more tables, based on a related column
between them.
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/ 6/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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:
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:
RIGHT JOIN:
https://www.mygreatlearning.com/blog/sql-interview-questions/ 7/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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:
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:
SELF JOIN:
The SELF JOIN joins a table to itself; temporarily renaming at least one table in the SQL
statement.
SYNTAX:
Joins in SQL
https://www.mygreatlearning.com/blog/sql-interview-questions/ 8/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
https://www.mygreatlearning.com/blog/sql-interview-questions/ 9/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Primary Key is a constraint in SQL. So, before understanding what exactly is a primary key,
let’s understand what exactly is a constraint in SQL. Constraints are the rules enforced on
data columns on a table. These are used to limit the type of data that can go into a table.
Constraints can either be column level or table level.
Let’s look at the different types of constraints which are present in SQL:
Constraint Description
CHECK The CHECK constraint ensures that all values in a column satisfy certain conditions.
Used to create and retrieve data from the database very quickly.
INDEX
You can consider the 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 column
cannot have any null values present in it and also all the values present in this column
must be unique.
Show tables;
What is PL/SQL?
PL SQL stands for Procedural language constructs for Structured Query Language. PL SQL
was introduced by Oracle to overcome the limitations of plain sql. So, pl sql adds in
procedural language approach to the plain vanilla sql.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 10/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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. However, if you wish to learn more
about Oracle, you can also take up free oracle courses and enhance your knowledge.
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.
Different database management systems have different queries to see all the tables.
To see all the tables in MYSQL, we would have to use this query:
show tables;
SELECT
table_name
FROM
User_tables;
SELECT
*
FROM
Information_schema.tables;
ETL stands for Extract, Transform and Load. It is a three-step process, where we would have
to start off by extracting the data from sources. Once we collate the data from different
sources, what we have is raw data. This raw data has to be transformed into the tidy
format, which will come in the second phase. Finally, we would have to load this tidy data
into tools which would help us to find insights.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 11/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
SQL stands for Structured Query Language and it is not something you can install. To
implement sql queries, you would need a relational database management system. There
are different varieties of relational database management systems such as:
ORACLE
MYSQL
SQL Server
Hence, to implement sql queries, we would need to install any of these Relational Database
Management Systems.
The update command comes under the DML(Data Manipulation Langauge) part of sql and
is used to update the existing data in the table.
UPDATE employees
SET last_name=‘Cohen’
WHERE employee_id=101;
With this update command, I am changing the last name of the employee.
Rename column in SQL: 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.
Command Description
https://www.mygreatlearning.com/blog/sql-interview-questions/ 12/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Triggers may implement DML by using INSERT, UPDATE, and DELETE statements. These
triggers that contain DML and find other triggers for data modification are called Nested
Triggers.
By using BETWEEN in the where clause, we can retrieve the Employee Ids of employees with
salary >= 20000 and <=10000.
“Order by 2” is valid when there are at least 2 columns used in SELECT statement. Here this
query will throw error because only one column is used in the SELECT statement.
What is OLTP?
OLTP stands for Online Transaction Processing. And is a class of software applications
capable of supporting transaction-oriented programs. An essential attribute of an OLTP
https://www.mygreatlearning.com/blog/sql-interview-questions/ 13/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Data Integrity is the assurance of accuracy and consistency of data over its entire life-
cycle, and is a critical aspect to the design, implementation and usage of any system
which stores, processes, or retrieves data. It also defines integrity constraints to enforce
business rules on the data when it is entered into an application or a database.
What is OLAP?
OLAP stands for Online Analytical Processing. And a class of software programs which are
characterized by relatively low frequency of online transactions. Queries are often too
complex and involve a bunch of aggregations.
There are so many times where user needs to find out the specific constraint information
of the table. The following queries are useful, SELECT * From User_Constraints; SELECT *
FROM User_Cons_Columns;
1. ROWCOUNT function
2. Set rowcount 3
3. Select * from employee order by empid desc Set rowcount 0
Error. Operand data type NULL is invalid for the Avg operator.
The output of Cross Join is called a Cartesian product. It returns rows combining each row
from the first table with each row of the second table. For Example, if we join two tables
having 15 and 20 columns the Cartesian product of two tables will be 15×20=300 rows.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 14/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Our database comprises of a lot of different entities such as tables, stored procedures,
functions, database owners and so on. To make sense of how all these different entities
interact, we would need the help of schema. So, you can consider schema to be the logical
relationship between all the different entities which are present in the database.
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.
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.
Unique Key is a constraint in SQL. So, before understanding what exactly is a primary key,
let’s understand what exactly is a constraint in SQL. Constraints are the rules enforced on
data columns on a table. These are used to limit the type of data that can go into a table.
Constraints can either be column level or table level.
Unique Key:
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.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 15/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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.
BASIS FOR
SQL PL/SQL
COMPARISON
In SQL you can execute a single In PL/SQL you can execute a block of code
Basic
query or a command at a time. at a time.
In SQL you can write queries and In PL/SQL you can write a block of code
Writes commands using DDL, DML that has procedures, functions, packages
statements. or variables, etc.
Using SQL, you can retrieve, Using PL/SQL, you can create applications
modify, add, delete, or or server pages that display the
Use
manipulate the data in the information obtained from SQL in a proper
database. format.
S.
Where Clause Having Clause
No.
1 The WHERE clause specifies the criteria which The HAVING clause cannot be used
individual records must meet to be selected by without the GROUP BY clause
https://www.mygreatlearning.com/blog/sql-interview-questions/ 16/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
The WHERE clause cannot contain aggregate The HAVING clause can contain
3
functions aggregate functions
SELECT Columnq,
SELECT Column,AVG(Column_nmae)FROM AVG(Coulmn_nmae)FROM Table_name
5 Table_name WHERE Column > value GROUP BY WHERE Column > value GROUP BY
Column_nmae Column_nmae Having
column_name>or<value
SQL injection is a hacking technique which is widely used by black-hat hackers to steal
data from your tables or databases. Let’s say, if you go to a website and give in your user
information and password, the hacker would add some malicious code over there such
that, he can get the user information and password directly from the database. If your
database contains any vital information, it is always better to keep it secure from SQL
injection attacks.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 17/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 18/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
(
'Bob',
26,
90000
);
In the above example, we are inserting multiple records into the table called employees.
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:
We can use the SELECT INTO statement to copy data from one table to another. Either we
can copy all the data or only some specific columns.
This is how we can copy all the columns into a new table:
SELECT *
INTO newtable
https://www.mygreatlearning.com/blog/sql-interview-questions/ 19/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
FROM oldtable
WHERE condition;
We can add a new column in SQL with the help of alter command:
This command helps us to add a new column named as contact in the employees table.
The LIKE operator checks if an attribute value matches a given string pattern. Here is an
example of LIKE operator
With this command, we will be able to extract all the records where the first name is like
“Steven”.
Yes, SQL server drops all related objects, which exists inside a table like constraints, indexex,
columns, defaults etc. But dropping a table will not drop views and sorted procedures as
they exist outside the table.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 20/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Yes, we can disable a single trigger on the database by using “DISABLE TRIGGER
triggerName ON<>. We also have an option to disable all the trigger by using, “DISABLE
Trigger ALL ON ALL SERVER”.
A live lock is one where a request for an exclusive lock is repeatedly denied because a
series of overlapping shared locks keep interferring. A live lock also occurs when read
transactions create a table or page.
Records can be fetched for both Odd and Even row numbers – To display even numbers –
When a COMMIT is used in a transaction, all changes made in the transaction are written
into the database permanently.
Example:
A table can be joined to itself using self join, when you want to create a result set that joins
records in a table with other records in the same table.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 21/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
When two or more tables have been joined using equal to operator then this category is
called an equi join. Just we need to concentrate on the condition is equal to (=) between
the columns in the table.
Example:
The SELECT DISTINCT is used to get distinct data from tables using a query. The below SQL
query selects only the DISTINCT values from the “Country” column in the “Customers” table:
Here, we are copying the student table to another table with the same structure with no
rows copied.
When two or more tables are joining without equal to condition, then that join is known as
Non Equi Join. Any operator can be used here, that is <>,!=,<,>,Between.
Example:
https://www.mygreatlearning.com/blog/sql-interview-questions/ 22/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
By using the SET ROWCOUNT command. It limits the number of records affected by a
command. Let’s take an example, if you have 2 duplicate rows, you would SET ROWCOUNT 1,
execute DELETE command and then SET ROWCOUNT 0.
Both the NVL(exp1, exp2) and NVL2(exp1, exp2, exp3) functions check the value exp1 to see if
it is null. With the NVL(exp1, exp2) function, if exp1 is not null, then the value of exp1 is
returned; otherwise, the value of exp2 is returned, but case to the same data type as that
of exp1. With the NVL2(exp1, exp2, exp3) function, if exp1 is not null, then exp2 is returned;
otherwise, the value of exp3 is returned.
The given syntax indicates that the user can grant access to another user too.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 23/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
It compresses the MyISAM tables, which reduces their disk or memory usage.
What is ISAM?
White box testing includes: Database Consistency and ACID properties Database triggers
and logical views Decision Coverage, Condition Coverage, and Statement Coverage
Database Tables, Data Model, and Database Schema Referential integrity rules.
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.
External Access Sandbox: Users can access files without having the right to manipulate the
memory allocation.
Unsafe Access Sandbox: This contains untrusted codes where a user can have access to
memory.
Data Mapping
Data stored and retrieved
Use of Black Box testing techniques such as Equivalence Partitioning and Boundary Value
Analysis (BVA).
This join is usable, when user wants all the records from Right table (Second table) and
only equal or matching records from First or left table. The unmatched records are
considered as null records. Example: Select t1.col1,t2.col2….t ‘n’col ‘n.’. from table1 t1,table2 t2
where t1.col(+)=t2.col;
What is a Subquery?
https://www.mygreatlearning.com/blog/sql-interview-questions/ 24/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
A SubQuery is a SQL query nested into a larger query. Example: SELECT employeeID,
firstName, lastName FROM employees WHERE departmentID IN (SELECT departmentID
FROM departments WHERE locationID = 2000) ORDER BY firstName, lastName;
There are multiple ways to find duplicate records in SQL. Let’s see how can we find
duplicate records using group by:
SELECT
x,
y,
COUNT(*) occurrences
FROM z1
GROUP BY
x,
y
HAVING
COUNT(*) > 1;
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
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
https://www.mygreatlearning.com/blog/sql-interview-questions/ 25/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
ELSE result
END;
We start off by giving the CASE keyword, then we follow it up by giving multiple WHEN, THEN
statements.
delete e
from emp e
inner join
(select *,
RANK() OVER ( PARTITION BY eid,ename ORDER BY id DESC )rank
From emp )T on e.sid=t.sid
where e.Rank>1
Below is the syntax to delete duplicate records using groupby and min:
https://www.mygreatlearning.com/blog/sql-interview-questions/ 26/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
alter table emp
drop column sno
Cursors in SQL are used to store database tables. There are two types of cursors:
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.
If you have worked with other languages, then you would know about the concept of
Functions. You can consider stored procedures in SQL to be analogous to functions in other
languages. This means that we can store a SQL statement as a stored procedure and this
stored procedure can be invoked whenever we want.
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.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 27/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
EXEC procedure_name;
We will give in the keyword EXEC and then give the name of the stored procedure.
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
the table on which we would want to create this index. Finally, in parenthesis, we will list out
all the columns which will have the index. Let’s look at an example:
In the above example, we are creating an index called a salary on top of the ‘Salary’
column of the ‘Employees’ table.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 28/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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.
We can change the data type of the column using the alter table. This will be the
command:
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.
SQL stands for structured query language and is majorly used to query data from
relational databases. When we talk about a SQL database, it will be a relational database.
But when it comes to the NoSQL databases, we will be working with non-relational
databases.
Want to learn more about NoSQL databases? Check out the NoSQL course.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 29/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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.
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:
https://www.mygreatlearning.com/blog/sql-interview-questions/ 30/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Complex View:
Inline View:
A subquery is also called an inline view if and only if it is called in FROM clause of a SELECT
query.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 31/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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.
The BETWEEN operator checks an attribute value within a range. Here is an example of
BETWEEN operator:
With this command, we will be able to extract all the records where the salary of the
employee is between 10000 and 20000.
DDL (Data Definition Language): Used to define the data structure it consists of the
commands like CREATE, ALTER, DROP, etc.
DML (Data Manipulation Language): Used to manipulate already existing data in the
database, commands like SELECT, UPDATE, INSERT
DCL (Data Control Language): Used to control access to data in the database, commands
like GRANT, REVOKE.
CHAR is used to store fixed-length character strings, and VARCHAR2 is used to store
variable-length character strings.
By using the column alias in the ORDER BY instead of where clause for sorting
COALESCE() accepts two or more parameters, one can apply 2 or as many parameters but
it returns only the first non NULL parameter.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 32/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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.
A trigger allows you to execute a batch of SQL code when an insert,update or delete
command is run against a specific table as Trigger is said to be the set of actions that are
performed whenever commands like insert, update or delete are given.
Aggregate functions are used to evaluate mathematical calculations and return single
values. This can be calculated from the columns in a table. Scalar functions return a single
value based on input value.
What is a deadlock?
It is an unwanted situation where two or more transactions are waiting indefinitely for one
another to release the locks.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 33/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Left outer join is useful if you want all the records from the left table(first table) and only
matching records from 2nd table. The unmatched records are null records. Example: Left
outer join with “+” operator Select t1.col1,t2.col2….t ‘n’col ‘n.’. from table1 t1,table2 t2 where
t1.col=t2.col(+);
The UNION operator combines the results of two or more Select statements by removing
duplicate rows. The columns and the data types must be the same in the SELECT
statements.
SQL Constraints are used to specify the rules of data type in a table. They can be specified
while creating and altering the table. The following are the constraints in SQL: NOT NULL
CHECK DEFAULT UNIQUE PRIMARY KEY FOREIGN KEY
This command provides another name to a table or a column. It can be used in the WHERE
clause of a SQL query using the “as” keyword.
Group functions work on a set of rows and return a single result per group. The popularly
used group functions are AVG, MAX, MIN, SUM, VARIANCE, and COUNT.
This function is used to convert the NULL value to the other value.
SELECT EmpNo, EmpName, Salary FROM employee WHERE deptNo in (select deptNo from
dept where deptName = ‘ECE’)
What are the main differences between #temp tables and @table
variables and which one is preferred?
What is CLAUSE?
SQL clause is defined to limit the result set by providing conditions to the query. This usually
filters some rows from the whole set of records. Example – Query that has WHERE condition.
A stored procedure 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.
The Bulk Copy is a utility or a tool that exports/imports data from a table into a file and vice
versa.
In SQL cross join, a combination of every row from the two tables is included in the result
set. This is also called cross product set. For example, if table A has ten rows and table B
has 20 rows, the result set will have 10 * 20 = 200 rows provided there is a NOWHERE clause
in the SQL statement.
LIKE operator is used for pattern matching, and it can be used as- 1. % – Matches zero or
more characters. 2. _(Underscore) – Matching exactly one character.
SELECT CURDATE();
https://www.mygreatlearning.com/blog/sql-interview-questions/ 35/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
A query first takes the lowest level lock possible with the smallest row level. When too 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.
The order of SQL SELECT clauses is: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY. Only
the SELECT and FROM clauses are mandatory.
IN: Works on List result set Doesn’t work on subqueries resulting in Virtual tables with
multiple columns Compares every value in the result list.
Exists: Works on Virtual tables Is used with co-related queries Exits comparison when the
match is found
INSERT INTO table2 (column1, column2, column3, …) SELECT column1, column2, column3, …
FROM table1 WHERE condition;
List the ACID properties that make 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.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 36/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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.
This command is used to provide database access to users other than the administrator
in SQL privileges.
First Normal Form (1NF): It removes all duplicate columns from the table. It creates a table
for related data and identifies unique column values.
INSERT syntax is used to add a record to the table. INSERT into table_name VALUES (value1,
value2..);
MyISAM
Heap
Merge
INNO DB
ISAM
https://www.mygreatlearning.com/blog/sql-interview-questions/ 37/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
BLOB stands for the large binary objects. It is used to hold a variable amount of data. TEXT
is a case-insensitive BLOB. TEXT values are non-binary strings (character strings).
Mysql_close() cannot be used to close the persistent connection. Though it can be used to
close a connection opened by mysql_connect().
In day-to-day activities, the user needs to find out the data between some range. To
achieve this user needs to use Between..and operator or Greater than and less than the
operator.
There are so many system tables which are very important. Using the system table user
can count the number of rows in the table. following query is helpful in that case, Select
table_name, num_rows from user_tables where table_name=’Employee’;
The following query will not fetch a record with the salary of 6000 but also will skip the
record with NULL.
Yes, no error. The output will be NULL. Performing any operation on NULL will get the NULL
result.
SQL server has stayed on top as one of the most popular database management products
ever since its first release in 1989 by Microsoft Corporation. The product is used across
industries to store and process large volumes of data. It was primarily built to store and
process data that is built on a relational model of data.
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.
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
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.
Click on the Download Now button.
Save the .exe file on your system. Right-click on the .exe file and click on Open.
Click on ‘Yes’ to allow the changes to be made on your system and have SQL Server Installed.
Once the installation is complete, restart your system, if required, and launch the SQL Server
Management Studio application from the START menu.
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 saved within the Stored Procedure.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 39/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Launch the SQL Server Management Studio application and from the Object Explorer
window pane, right-click on Databases and click on Restore. This would automatically
restore the database.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 40/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Launch the SQL Server Management Studio. Go to the Database for which you require the
Connection string. Right-click on the database and click on Properties. In the Properties
window that is displayed, you can view the Connection String property.
Connection strings help connect databases to another staging database or any external
source of data.
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:
1 WITH RESULT AS
2 (SELECT COL1, COL2, COL3
3 FROM EMPLOYEE)
4 SELECT COL1, COL2 FROM RESULT
5 CTEs can be used with Insert, Update or Delete statements as well.
with result as
In this way, CTEs can be used to find the nth highest salary within an organisation.
Launch your SQL Server Management Studio. Click on the Database connection for which
you want to change the login password. Click on Security from the options that get
displayed.
Click on Logins and open your database connection. Type in the new password for login
and click on ‘OK’ to apply the changes.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 42/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
In Windows 10, go to the START menu and locate the SQL Server.
You can run the below query to view the current version of SQL Server that you are using.
1 SELECT @@version;
From the Object Explorer window pane, go to the table where the column is present and
choose Design. Under the Column Name, select the name you want to rename and enter
the new name. Go to the File menu and click Save.
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 saved within the Stored Procedure.
You can execute the Stored Proc by using the command Exec Procedure_Name;
After installing the required version of SQL Server, it is easy to create new databases and
maintain them.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 43/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Indexes are database objects which help in retrieving records quickly and more efficiently.
Column indexes can be created on both Tables and Views. By declaring a Column as an
index within a table/ view, the user can access those records quickly by executing the
index. Indexes with more than one column are called Clustered indexes.
Syntax:
Tables are the fundamental storage objects within a database. A table is usually made up
of
Rows and Columns. The below syntax can be used to create a new table with 3 columns.
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.
Launch the SQL Server Management Studio from the START menu.
In the dialogue box shown below, select the Server Type as Database Engine and Server
Name as the name of your laptop/ desktop system.
Select the appropriate Authentication type and click on the Connect button.
A secure connection would be established, and the list of the available Databases will be
loaded in the Object Explorer window pane.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 44/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
The Express and Developer versions (open-source versions) of the latest SQL Server
release can be downloaded from the official Microsoft website. The link is given below for
reference.
https://www.microsoft.com/en-in/sql-server/sql-server-downloads
Launch the SQL Server Management Studio from the START menu.
In the dialogue 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.
Select the Authentication as ‘Windows Authentication.
A secure connection would be established, and the list of the available Databases will be
loaded in the Object Explorer window pane.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 45/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Both the Express and Developer versions (free editions) of SQL Server can be downloaded
from the official Microsoft website. The link is given below for reference.
Click on the link below: https://www.microsoft.com/en-in/sql-server/sql-server-downloads
Click on the search icon and type in – SQL Server 2014 download
Click on the result link to download and save SQL Server 2014.
From the START menu, type SQL Server. Right-click on the app and select uninstall to
uninstall the application from your system. Restart the system, if required, for the changes
to get affected.
Run the query SELECT @@version; to find the version and name of the SQL Server you are
using.
Launch the SQL Server Management Studio from the START menu. Login using Windows
Authentication. In the Object Explorer window pane, you can view the list of databases and
corresponding objects.
Case When statements in SQL are used to run through many conditions and to return a
value when one such condition is met. If none of the conditions is met in the When
statements, then the value mentioned in the Else statement is returned.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 46/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
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
when region_id >= 5 AND region_id <= 7 then
1
else
0
end ) as Canada
from company_regions;
Nested CASE statement:
SELECT
SUM (
CASE
WHEN rental_rate = 0.99 THEN
1
ELSE
0
END
) AS "Mass",
SUM (
CASE
WHEN rental_rate = 2.99 THEN
1
ELSE
0
END
) AS "Economic",
SUM (
CASE
https://www.mygreatlearning.com/blog/sql-interview-questions/ 47/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Launch Google and in the Search toolbar, type in SQL Server Management Studio
download.
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.
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 saved within the Stored Procedure.
You can execute the Stored Proc by using the command Exec Procedure_Name;
Launch the SQL Server Management Studio from the START menu. Login using Windows
Authentication. In the Object Explorer window pane, you can view the list of databases and
corresponding objects.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 48/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
SQL Server is used to retrieve and process various data that is built on a relational model.
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 frameworks using
Connection Strings.
SQL Server can also be used in conjunction with Big Data tools like Hadoop.
Functions are pre-written codes that return a value and which help the user achieve a
particular task concerning viewing, manipulating, and processing data.
AGGREGATE FUNCTIONS:
STRING FUNCTIONS:
COALESCE()
CAST()
CONCAT()
SUBSTRING()
DATE FUNCTIONS:
GETDATE()
DATEADD()
DATEDIFF()
There are many types of functions such as Aggregate Functions, Date Functions, String
Functions, Mathematical functions, etc.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 49/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Query to find the 10 highest salaries. For up-gradation of the b10 band.
with result as
In this way, by replacing the salary rank value, we can find the nth highest salary in any
organisation.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 50/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Temporary tables can be used to retain the structure and a subset of data from the
original table from which they were derived.
Syntax:
Temporary tables do not occupy any physical memory and can be used to retrieve data
faster.
PostgreSQL is one of the most widely and popularly used languages for Object-Relational
Database Management systems. It is mainly used for large web applications. It is an open-
source, object-oriented, -relational database system. It is extremely powerful and enables
users to extend any system without problem. It extends and uses the SQL language in
combination with various features for safely scaling and storage of intricate data
workloads.
UUID
Numeric types
Boolean
Character types
Temporal types
Geometric primitives
Arbitrary precision numeric
XML
Arrays etc
Indices in PostgreSQL allow the database server to find and retrieve specific rows in a
given structure. Examples are B-tree, hash, GiST, SP-GiST, GIN and BRIN. Users can also
define their indices in PostgreSQL. However, indices add overhead to the data
manipulation operations and are seldom used
https://www.mygreatlearning.com/blog/sql-interview-questions/ 51/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Tokens in PostgreSQL act as the building blocks of a source code. They are composed of
various special character symbols. Commands are composed of a series of tokens and
terminated by a semicolon(“;”). These can be a constant, quoted identifier, other
identifiers, keyword or a constant. Tokens are usually separated by whitespaces.
Various options can be taken by the createDB command based on the use case.
You can create a new table by specifying the table name, along with all column names
and their types:
The column the data type can be changed in PostgreSQL by using the ALTER TABLE
command:
https://www.mygreatlearning.com/blog/sql-interview-questions/ 52/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
PostgreSQL MongoDB
PostgreSQL is an SQL database where data is MongoDB, on the other hand, is a NoSQL
stored as tables, with structured rows and database. There is no requirement for a
columns. It supports concepts like referential schema, therefore it can store unstructured
integrity entity-relationship and JOINS. data. Data is stored as BSON documents and
PostgreSQL uses SQL as its querying language. the document’s structure can be changed by
PostgreSQL supports vertical scaling. This the user. MongoDB uses JavaScript for querying.
means that you need to use big servers to It supports horizontal scaling, as a result of
store data. This leads to a requirement of which additional servers can be added as per
downtime to upgrade. It works better if you the requirement with minimal to no downtime.
require relational databases in your It is appropriate in a use case that requires a
application or need to run complex queries highly scalable distributed database that stores
that test the limit of SQL. unstructured data
Please note that only databases having no active connections can be dropped.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 53/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Schemas are a part of the database that contains tables. They also contain other kinds of
named objects, like data types, functions, and operators.
The object names can be used in different schemas without conflict; Unlike databases,
schemas are separated more flexibly. This means that a user can access objects in any of
the schemas in the database they are connected to, till they have privileges to do so.
Schemas are highly beneficial when there is a need to allow many users access to one
database without interfering with each other. It helps in organizing database objects into
logical groups for better manageability. Third-party applications can be put into separate
schemas to avoid conflicts based on names.
It is denoted by ‘|/” and returns the square root of a number. Its syntax is
1 Select |/ <number>
What Is A Candid?
The CTIDs field exists in every PostgreSQL table. It is unique for every record of a table and
exactly shows the location of a tuple in a particular table. A logical row’s CTID changes
when it is updated, thus it cannot be used as a permanent row identifier. However, it is
useful when identifying a row within a transaction when no update is expected on the data
item.
It is denoted by ‘|/” and returns the square root of a number. Its syntax is
1 Select |/ <number>
https://www.mygreatlearning.com/blog/sql-interview-questions/ 54/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
A non-clustered index in PostgreSQL is a simple index, used for fast retrieval of data, with
no certainty of the uniqueness of data. It also contains pointers to locations where other
parts of data are stored
Network-level security uses Unix Domain sockets, TCP/IP sockets, and firewalls.
Transport-level security which uses SSL/TLS to enable secure communication with the
database
Database-level security with features like roles and permissions, row-level security (RLS), and
auditing.
PART 1
This covers SQL basic query operations like creating databases forms scratch, creating a
table, inserting values etc.
It is better to get hands-on in order to have practical experience with SQL queries. A small
error/bug will make you feel surprised and next time you will get there!
2) Create a table with the name “bank_details” with the following columns
https://www.mygreatlearning.com/blog/sql-interview-questions/ 55/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
1 Describe bank_details;
— Product: PayCard
— Quantity: 3
— price: 330
— Puchase_cost: 8008
— estimated_sale_price: 9009
— Product: PayPoints —
— Quantity: 4
— price: 200
— Puchase_cost: 8000
— estimated_sale_price: 6800
5) Add a column: Geo_Location to the existing Bank_details table with data type varchar
and size 20
https://www.mygreatlearning.com/blog/sql-interview-questions/ 56/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
7) How many characters does the Product : “paycard” have in the Bank_details table.
9) Reduce the size of the Product field from 10 to 6 and check if it is possible
— c) End_time field which also displays hours and minutes and timezone
1 -- Step1:
2 Insert into bank_holidays values ( current_date(),
3 current_date(),
4 current_date() );
5
6 -- Step 2:
7 Update bank_holidays
8 set holiday = DATE_ADD(Holiday , INTERVAL 1 DAY);
https://www.mygreatlearning.com/blog/sql-interview-questions/ 57/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
15) Display the first five characters of the Geo_location field of Bank_details.
PART 2
— ——————————————————–
— ——————————————————–
Find all the players who were present in the test match 1 as well as in the test match 2.
Write a MySQl query to find the players from the test match 1 having popularity higher than
the average popularity.
1 select player_name , Popularity from cricket_1 WHERE Popularity > (SELECT AVG(Popular
Find player_id and player name that are common in the test match 1 and test match 2.
Retrieve player_id, runs, and player_name from cricket_1 and cricket_2 table and display
the player_id of the players where the runs are more than the average runs.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 58/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
1 SELECT player_id , runs , player_name FROM cricket_1 WHERE cricket_1.RUNS > (SELECT
Write a query to extract the player_id, runs and player_name from the table “cricket_1”
where the runs are greater than 50.
Write a query to extract all the columns from cricket_1 where player_name starts with ‘y’
and ends with ‘v’.
Write a query to extract all the columns from cricket_1 where player_name does not end
with ‘t’.
Write a MySQL query to create a new column PC_Ratio that contains the popularity to
charisma ratio.
Write a MySQL query to find the top 5 players having the highest popularity to charisma
ratio
Write a MySQL query to find the player_ID and the name of the player that contains the
character “D” in it.
Extract the Player_Id and Player_name of the players where the charisma value is null.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 59/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Separate all Player_Id into single numeric ids (example PL1 = 1).
Write a MySQL query to extract Player_Id, Player_Name and charisma where the charisma
is greater than 25.
1 SELECT Player_Id , Player_Name , charisma FROM new_cricket WHERE charisma > 25;
Write a query to count all the duplicate values from the column “Agreement” from the
table churn1.
Write a query to create a new column new_Amount that contains the sum of TotalAmount
and MonthlyServiceCharges.
Write a query to extract the customerID, InternetConnection and gender from the table
“Churn_Details ” where the value of the column “InternetConnection” has ‘i’ at the second
position.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 60/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Find the records where the tenure is 6x, where x is any number.
Part 3
# DataBase = Property Price Train
Write An MySQL Query To Print The First Three Characters Of Exterior1st From
Property_Price_Train_new Table.
Write MySQl query to print nearest largest integer value of column Garage_Area from
Property_Price_Train_new
https://www.mygreatlearning.com/blog/sql-interview-questions/ 61/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
1 SELECT CASE WHEN Underground_Full_Bathroom = 0 THEN 'false' ELSE 'true' END FROM Prop
Write MySQL query to extract Year_Sold, Sale_Price whose price is greater than 100000.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 62/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
# DATABASE Cricket
1 SELECT
2 cric2.Player_Id, cric2.Player_Name, cric2.Runs, cric2.Charisma, cric1.Popularity
3 FROM
4 cricket_1 AS cric1
5 RIGHT JOIN
6 cricket_2 AS cric2 ON cric1.Player_Id = cric2.Player_Id;
1 SELECT
2 cric1.Player_Id, cric1.Player_Name, cric1.Runs, cric1.Popularity, cric2.Charisma
3 FROM
4 cricket_1 AS cric1
5 LEFT JOIN
6 cricket_2 AS cric2 ON cric1.Player_Id = cric2.Player_Id;
1 SELECT
2 cric1.Player_Id, cric1.Player_Name, cric1.Runs, cric1.Popularity, cric2.Charisma
3 FROM
4 cricket_1 AS cric1
5 INNER JOIN
6 cricket_2 AS cric2 ON cric1.Player_Id = cric2.Player_Id;
Create a new table and insert the result obtained after performing inner join on the two
tables cricket_1 and cricket_2.
Write MySQL query to extract maximum runs of players get only top two players
https://www.mygreatlearning.com/blog/sql-interview-questions/ 63/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
1 select Player_Name, Runs from cricket_1 group by Player_Name having max(Runs) limit 2
PART 4
# Pre-Requisites
# Assuming Candidates are familiar with “Group by” and “Grouping functions” because
these are used along with JOINS in the questionnaire.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 65/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
119 INSERT INTO BANK_ACCOUNT_TRANSACTION VALUES ( "5800-1700-9800-7755", -9000, "POS-Wa
120 INSERT INTO BANK_ACCOUNT_TRANSACTION VALUES ( '5890-1970-7706-8912', -11000, "Shopp
121
122
123
124 # CREATE TABLE BANK_CUSTOMER_MESSAGES
125
126 CREATE TABLE BANK_CUSTOMER_MESSAGES (
127 Event VARCHAR(24),
128 Customer_message VARCHAR(75),
129 Notice_delivery_mode VARCHAR(15)) ;
130
131
132 INSERT INTO BANK_CUSTOMER_MESSAGES VALUES ( "Adhoc", "All Banks are closed due to a
133 INSERT INTO BANK_CUSTOMER_MESSAGES VALUES ( "Transaction Limit", "Only limited with
134 INSERT INTO `bank_account_transaction`(`Account_Number`, `Transaction_amount`, `Tra
135 ('4000-1956-9977' , 10000.00 ,'ECS transfer', 'MN' , '2020-02-16' ) ;
136
137 -- inserted for queries after 17th
138 INSERT INTO `bank_account_transaction`(`Account_Number`, `Transaction_amount`, `Tra
139 ('4000-1956-9977' , 40000.00 ,'ECS transfer', 'MN' , '2020-03-18' ) ;
140
141 INSERT INTO `bank_account_transaction`(`Account_Number`, `Transaction_amount`, `Tra
142 ('4000-1956-9977' , 60000.00 ,'ECS transfer', 'MN' , '2020-04-18' ) ;
143
144 INSERT INTO `bank_account_transaction`(`Account_Number`, `Transaction_amount`, `Tra
145 ('4000-1956-9977' , 20000.00 ,'ECS transfer', 'MN' , '2020-03-20' ) ;
146
147 -- inserted for queries after 24th
148
149 INSERT INTO `bank_account_transaction`(`Account_Number`, `Transaction_amount`, `Tra
150 ('4000-1956-9977' , 49000.00 ,'ECS transfer', 'MN' , '2020-06-18' ) ;
151
152
153
154
155 # CREATE TABLE BANK_INTEREST_RATE
156
157 CREATE TABLE BANK_INTEREST_RATE(
158 account_type varchar(24),
159 interest_rate decimal(4,2),
160 month varchar(2),
161 year varchar(4)
162 ) ;
163
164 INSERT INTO BANK_INTEREST_RATE VALUES ( "SAVINGS" , 0.04 , '02' , '2020' );
165 INSERT INTO BANK_INTEREST_RATE VALUES ( "RECURRING DEPOSITS" , 0.07, '02' , '2020'
166 INSERT INTO BANK_INTEREST_RATE VALUES ( "PRIVILEGED_INTEREST_RATE" , 0.08 , '02'
167
168
169 # Bank_holidays:
170
171 Insert into bank_holidays values( '2020-05-20', now(), now() ) ;
172
173 Insert into bank_holidays values( '2020-03-13' , now(), now() ) ;
Print customer Id, customer name and average account_balance maintained by each
customer for all of his/her accounts in the bank.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 66/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
# for all the transactions occurred during march,2020 and april, 2020
1 Select
2 ba.Account_Number, Balance_amount, Transaction_amount, Transaction_Date
3 from Bank_Account_Details ba
4 inner join
5 bank_account_transaction bat
6 on ba.account_number = bat.account_number
7 And ( Transaction_Date between "2020-03-01" and "2020-04-30");
8 -- or use below condition --
9 # (date_format(Transaction_Date , '%Y-%m') between "2020-03" and "2020-04");
Print all of the customer id, account number, balance_amount, transaction_amount from
bank_customer,
1 Select
2 ba.Customer_id,
3 ba.Account_Number, Balance_amount, Transaction_amount, Transaction_Date
4 from Bank_Account_Details ba
5 Left join bank_account_transaction bat
6 on ba.account_number = bat.account_number
7 And NOT ( date_format(Transaction_Date , '%Y-%m') = "2020-03" );
https://www.mygreatlearning.com/blog/sql-interview-questions/ 67/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Print only the customer id, customer name, account_number, balance_amount who did
transactions during the first quarter.
# Do not display the accounts if they have not done any transactions in the first quarter.
1 Select
2 ba.Customer_id,
3 ba.Account_Number, Balance_amount , transaction_amount , transaction_date from
4 Bank_Account_Details ba
5 Inner join bank_account_transaction bat
6 on ba.account_number = bat.account_number
7 And ( date_format(Transaction_Date , '%Y-%m') <= "2020-03" );
1 SELECT
2 ba.Customer_id,
3 ba.Account_Number,
4 (Balance_amount + IFNULL(transaction_amount, 0)) deducted_balance_amount
5
6 FROM Bank_Account_Details ba
7 LEFT JOIN bank_account_transaction bat
8 ON ba.account_number = bat.account_number
9 AND Relationship_type = "P";
# b) Along with the first step, Display other columns with the corresponding linking
account number, account types
https://www.mygreatlearning.com/blog/sql-interview-questions/ 68/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
# b) Along with the first step, Display other columns with corresponding linking account
number, account types
# c) After retrieving all records of accounts and their linked accounts, display the
transaction amount of accounts appeared in another column.
Display all saving account holders have “Add-on Credit Cards” and “Credit cards”
1 SELECT
2 br1.Account_Number primary_account_number ,
3 br1.Account_type primary_account_type,
4 br2.Account_Number secondary_account_number,
5 br2.Account_type secondary_account_type
6 from bank_account_relationship_details br1
7 JOIN bank_account_relationship_details br2
8 on br1.Account_Number = br2.Linking_Account_Number
9 and br2.Account_type like '%Credit%' ;
Many online sources 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, many blogs such as this list all the probable questions that
an interviewer might ask.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 69/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
SQL is a vast topic and there is a lot to learn. But the most basic skills that an SQL
professional should know are:
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. Also take up a Oracle SQL to learn more.
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/ 70/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
CREATE DATABASE
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.
https://www.mygreatlearning.com/blog/sql-interview-questions/ 71/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
To Conclude
For anyone who is well-versed in SQL knows that it is the most widely used Database
language. Thus, the most essential part to learn is SQL for Data Science to power ahead in
your career.
Wondering where to learn the highly coveted in-demand skills for free? Check out the
courses on Great Learning Academy. Enroll in any course, learn the in-demand skill, and
get your free certificate. Hurry!
Free Resources
Free SQL Courses
Free NoSQL Courses
Free PostgresSQL Courses
Sharing is caring:
Neha Kaplish
https://www.mygreatlearning.com/blog/sql-interview-questions/ 72/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Top 8 Android Projects with Source Top 30 Excel formulas you should
Code know in 2023
Leave a Comment
Your email address will not be published. Required fields are marked *
Type here..
https://www.mygreatlearning.com/blog/sql-interview-questions/ 73/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Name*
Email*
Website
Save my name, email, and website in this browser for the next time I comment.
Post Comment »
Free Courses
https://www.mygreatlearning.com/blog/sql-interview-questions/ 74/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Java Programming
View More →
Blog Categories
Data Science
Artificial Intelligence
Career
Cybersecurity
Popular Courses
PGP In Management
View More →
Salary Blogs
Salary Calculator
https://www.mygreatlearning.com/blog/sql-interview-questions/ 75/76
6/30/23, 9:28 PM 180+ SQL Interview Questions [2023]- Great Learning
Interview Questions
View More →
Quick Links
About Us
Contact Us
Privacy Policy
Terms of Use
© 2013 - 2023 Great Lakes E-Learning Services Pvt. Ltd. All rights reserved
https://www.mygreatlearning.com/blog/sql-interview-questions/ 76/76