SQL Interview Questions CHEAT SHEET (2024) - InterviewBit
SQL Interview Questions CHEAT SHEET (2024) - InterviewBit
This guide will help you to brush up on your SQL skills, regain your confidence and
be job-ready!
Here, you will find a collection of real-world Interview questions asked in companies
like Google, Oracle, Amazon, and Microsoft, etc. Each question comes with a
perfectly written answer inline, saving your interview preparation time.
It also covers practice problems to help you understand the basic concepts of SQL.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 1 of 49
:
SQL Interview Questions
1. What is Database?
2. What is DBMS?
RDBMS stands for Relational Database Management System. The key difference
here, compared to DBMS, is that RDBMS stores data in the form of a collection of
tables, and relations can be defined between the common fields of these tables.
Most modern database management systems like MySQL, Microsoft SQL Server,
Oracle, IBM DB2, and Amazon Redshift are based on RDBMS.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 2 of 49
:
4. What is SQL?
SQL stands for Structured Query Language. It is the standard language for relational
database management systems. It is especially useful in handling organized data
comprised of entities (variables) and relations between different entities of the data.
A table is an organized collection of data stored in the form of rows and columns.
Columns can be categorized as vertical and rows as horizontal. The columns in a
table are called fields while the rows can be referred to as records.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 3 of 49
:
7. What are Constraints in SQL?
Constraints are used to specify the rules concerning data in the table. It can be
applied for single or multiple fields in an SQL table during the creation of the table or
after creating using the ALTER TABLE command. The constraints are:
NOT NULL - Restricts NULL value from being inserted into a column.
CHECK - Verifies that all values in a field satisfy a condition.
DEFAULT - Automatically assigns a default value if no value has been specified
for the field.
Get Ready with
UNIQUE Free Mock
- Ensures Coding
unique values to be inserted into the field.
Interview
INDEX - Indexes a field providing faster retrieval of records.
PRIMARY KEY - Uniquely identifies each record in a table.
FOREIGN KEY - Ensures referential integrity for a record in another table.
The PRIMARY KEY constraint uniquely identifies each row in a table. It must contain
UNIQUE values and has an implicit NOT NULL constraint.
A table in SQL is strictly restricted to have one and only one primary key, which is
comprised of single or multiple fields (columns).
CREATE TABLE Students ( /* Create table with a single field as primary key */
ID INT NOT NULL
Name VARCHAR(255)
PRIMARY KEY (ID)
);
CREATE TABLE Students ( /* Create table with multiple fields as primary key */
ID INT NOT NULL
LastName VARCHAR(255)
FirstName VARCHAR(255) NOT NULL,
CONSTRAINT PK_Student
PRIMARY KEY (ID, FirstName)
);
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 4 of 49
:
ALTER TABLE Students /* Set a column as primary key */
ADD PRIMARY KEY (ID);
ALTER TABLE Students /* Set multiple columns as primary key */
ADD CONSTRAINT PK_Student /*Naming a Primary Key*/
PRIMARY KEY (ID, FirstName);
A UNIQUE constraint ensures that all values in a column are different. This provides
uniqueness for the column(s) and helps identify each row uniquely. Unlike primary
key, there can be multiple unique constraints defined per table. The code syntax for
UNIQUE is quite similar to that of PRIMARY KEY and can be used interchangeably.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 5 of 49
:
A FOREIGN KEY comprises of single or collection of fields in a table that essentially
refers to the PRIMARY KEY in another table. Foreign key constraint ensures
referential integrity in the relation between two tables.
The table with the foreign key constraint is labeled as the child table, and the table
containing the candidate key is labeled as the referenced or parent table.
The SQL Join clause is used to combine records (rows) from two or more tables in a
SQL database based on a related column between the two.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 6 of 49
:
There are four different types of JOINs in SQL:
(INNER) JOIN: Retrieves records that have matching values in both tables
involved in the join. This is the widely used join for queries.
SELECT *
FROM Table_A
JOIN Table_B;
SELECT *
FROM Table_A
INNER JOIN Table_B;
LEFT (OUTER) JOIN: Retrieves all the records/rows from the left and the
matched records/rows from the right table.
SELECT *
FROM Table_A A
LEFT JOIN Table_B B
ON A.col = B.col;
RIGHT (OUTER) JOIN: Retrieves all the records/rows from the right and the
matched records/rows from the left table.
SELECT *
FROM Table_A A
RIGHT JOIN Table_B B
ON A.col = B.col;
FULL (OUTER) JOIN: Retrieves all the records where there is a match in either
the left or right table.
SELECT *
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 7 of 49
:
FROM Table_A A
FULL JOIN Table_B B
ON A.col = B.col;
Practice Problems
Solve these problems to ace this concept
A self JOIN is a case of regular join where a table is joined to itself based on some
relation between its own column(s). Self-join uses the INNER JOIN or LEFT JOIN
clause and a table alias is used to assign different names to the table within the
query.
Cross join can be defined as a cartesian product of the two tables included in the
join. The table after join contains the same number of rows as in the cross-product
of the number of rows in the two tables. If a WHERE clause is used in cross join then
the query will work like an INNER JOIN.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 8 of 49
:
CROSS JOIN subjects AS sub;
A database index is a data structure that provides a quick lookup of data in a column
or columns of a table. It enhances the speed of operations accessing data from a
database table at the cost of additional writes and memory to maintain the index
data structure.
There are different types of indexes that can be created for different purposes:
Unique indexes are indexes that help maintain data integrity by ensuring that no two
rows of data in a table have identical key values. Once a unique index has been
defined for a table, uniqueness is enforced whenever keys are added or changed
within the index.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 9 of 49
:
Non-unique indexes, on the other hand, are not used to enforce constraints on the
tables with which they are associated. Instead, non-unique indexes are used solely
to improve query performance by maintaining a sorted order of data values that are
used frequently.
Clustered indexes are indexes whose order of the rows in the database corresponds
to the order of the rows in the index. This is why only one clustered index can exist in
a given table, whereas, multiple non-clustered indexes can exist in the table.
The only difference between clustered and non-clustered indexes is that the
database manager attempts to keep the data in the database in the same order as
the corresponding keys appear in the clustered index.
Clustering indexes can improve the performance of most query operations because
they provide a linear-access path to data stored in the database.
As explained above, the differences can be broken down into three small factors -
Clustered index modifies the way records are stored in a database based on the
indexed column. A non-clustered index creates a separate entity within the table
which references the original table.
Clustered index is used for easy and speedy retrieval of data from the database,
whereas, fetching records from the non-clustered index is relatively slower.
In SQL, a table can have a single clustered index whereas it can have multiple
non-clustered indexes.
Data Integrity is the assurance of accuracy and consistency of data over its entire
life-cycle and is a critical aspect of the design, implementation, and usage of any
system which stores, processes, or retrieves data. It also defines integrity
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 10 of 49
:
constraints to enforce business rules on the data when it is entered into an
application or a database.
A subquery is a query within another query, also known as a nested query or inner
query. It is used to restrict or enhance the data to be queried by the main query,
thus restricting or enhancing the output of the main query respectively. For example,
here we fetch the contact information for students who have enrolled for the maths
subject:
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 11 of 49
:
the output of the subquery is substituted in the main query.
Practice Problems
Solve these problems to ace this concept
SELECT operator in SQL is used to select data from a database. The data returned is
stored in a result table, called the result-set.
Practice Problems
Solve these problems to ace this concept
20. What are some common clauses used with SELECT query in
SQL?
Some common SQL clauses used in conjuction with a SELECT query are as follows:
WHERE clause in SQL is used to filter records that are necessary, based on
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 12 of 49
:
specific conditions.
ORDER BY clause in SQL is used to sort the records based on some field(s) in
ascending (ASC) or descending order (DESC).
SELECT *
FROM myDB.students
WHERE graduation_year = 2019
ORDER BY studentID DESC;
GROUP BY clause in SQL is used to group records with identical data and can
be used in conjunction with some aggregation functions to produce summarized
results from the database.
HAVING clause in SQL is used to filter records in combination with the GROUP
BY clause. It is different from WHERE, since the WHERE clause cannot filter
aggregated records.
The UNION operator combines and returns the result-set retrieved by two or more
SELECT statements.
The MINUS operator in SQL is used to remove duplicates from the result-set
obtained by the second SELECT query from the result-set obtained by the first
SELECT query and then return the filtered results from the first.
The INTERSECT clause in SQL combines the result-set fetched by the two SELECT
statements where records from one match the other and then returns this
intersection of result-sets.
Certain conditions need to be met before executing either of the above statements in
SQL -
Each SELECT statement within the clause must have the same number of
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 13 of 49
:
columns
The columns must also have similar data types
The columns in each SELECT statement should necessarily have the same order
A database cursor is a control structure that allows for the traversal of records in a
database. Cursors, in addition, facilitates processing after traversal, such as
retrieval, addition, and deletion of database records. They can be viewed as a
pointer to one row in a set of rows.
1. DECLARE a cursor after any variable declaration. The cursor declaration must
always be associated with a SELECT Statement.
2. Open cursor to initialize the result set. The OPEN statement must be called
before fetching rows from the result set.
3. FETCH statement to retrieve and move to the next row in the result set.
4. Call the CLOSE statement to deactivate the cursor.
5. Finally use the DEALLOCATE statement to delete the cursor definition and
release the associated resources.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 14 of 49
:
DECLARE @name VARCHAR(50) /* Declare All Required Variables */
DECLARE db_cursor CURSOR FOR /* Declare Cursor Name*/
SELECT name
FROM myDB.students
WHERE parent_name IN ('Sara', 'Ansh')
OPEN db_cursor /* Open cursor and Fetch data into @name */
FETCH next
FROM db_cursor
INTO @name
CLOSE db_cursor /* Close the cursor and deallocate the resources */
DEALLOCATE db_cursor
Entity: An entity can be a real-world object, either tangible or intangible, that can be
easily identifiable. For example, in a college database, students, professors, workers,
departments, and projects can be referred to as entities. Each entity has some
associated properties that provide it an identity.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 15 of 49
:
24. List the different types of relationships in SQL.
An alias is represented explicitly by the AS keyword but in some cases, the same can
be performed without it as well. Nevertheless, using the AS keyword is always a
good practice.
A view in SQL is a virtual table based on the result-set of an SQL statement. A view
contains rows and columns, just like a real table. The fields in a view are fields from
one or more real tables in the database.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 16 of 49
:
27. What is Normalization?
Normal Forms are used to eliminate or reduce redundancy in database tables. The
different forms are as follows:
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 17 of 49
:
student in the table, has a name, his/her address, and the books they issued
from the public library -
Students Table
Windsor Street
Ansh Dracula (Bram Stoker) Mr.
777
As we can observe, the Books Issued field has more than one value per record, and
to convert it into 1NF, this has to be resolved into separate individual records for
each book issued. Check the following table in 1NF form -
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 18 of 49
:
Ansh Windsor Street 777 Dracula (Bram Stoker) Mr.
A relation is in second normal form if it satisfies the conditions for the first normal
form and does not contain any partial dependency. A relation in 2NF has no partial
dependency, i.e., it has no non-prime attribute that depends on any proper subset
of any candidate key of the table. Often, specifying a single column Primary Key is
the solution to the problem. Examples -
Example 1 - Consider the above example. As we can observe, the Students Table in
the 1NF form has a candidate key in the form of [Student, Address] that can uniquely
identify all records in the table. The field Books Issued (non-prime attribute)
depends partially on the Student field. Hence, the table is not in 2NF. To convert it
into the 2nd Normal Form, we will partition the tables into two while specifying a new
Primary Key attribute to identify the individual records in the Students table. The
Foreign Key constraint will be set on the other table to ensure referential integrity.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 19 of 49
:
3 Beautiful Bad (Annie Ward)
Here, WX is the only candidate key and there is no partial dependency, i.e., any
proper subset of WX doesn’t determine any non-prime attribute in the relation.
A relation is said to be in the third normal form, if it satisfies the conditions for the
second normal form and there is no transitive dependency between the non-prime
attributes, i.e., all non-prime attributes are determined only by the candidate keys of
the relation and not by any other non-prime attribute.
Example 1 - Consider the Students Table in the above example. As we can observe,
the Students Table in the 2NF form has a single candidate key Student_ID (primary
key) that can uniquely identify all records in the table. The field Salutation (non-
prime attribute), however, depends on the Student Field rather than the candidate
key. Hence, the table is not in 3NF. To convert it into the 3rd Normal Form, we will
once again partition the tables into two while specifying a new Foreign Key
constraint to identify the salutations for individual records in the Students table. The
Primary Key constraint for the same will be set on the Salutations table to identify
each record uniquely.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 20 of 49
:
4 Ansh Windsor Street 777 1
Salutation_ID Salutation
1 Ms.
2 Mr.
3 Mrs.
For the above relation to exist in 3NF, all possible candidate keys in the above
relation should be {P, RS, QR, T}.
A relation is in Boyce-Codd Normal Form if satisfies the conditions for third normal
form and for every functional dependency, Left-Hand-Side is super key. In other
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 21 of 49
:
words, a relation in BCNF has non-trivial functional dependencies in form X –> Y,
such that X is always a super key. For example - In the above example, Student_ID
serves as the sole unique identifier for the Students Table and Salutation_ID for the
Salutations Table, thus these tables exist in BCNF. The same cannot be said for the
Books Table and there can be several books with common Book Names and the
same Student_ID.
TRUNCATE command is used to delete all the rows from the table and free the
space containing the table.
DROP command is used to remove an object from the database. If you drop a table,
all the rows in the table are deleted and the table structure is removed from the
database.
If a table is dropped, all things associated with the tables are dropped as well. This
includes - the relationships defined on the table with other tables, the integrity
checks and constraints, access privileges and other grants that the table has. To
create and use the table again in its original form, all these relations, checks,
constraints, privileges and relationships need to be redefined. However, if a table is
truncated, none of the above problems exist and the table retains its original
structure.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 22 of 49
:
statements?
The TRUNCATE command is used to delete all the rows from the table and free the
space containing the table.
The DELETE command deletes only the rows from the table based on the condition
given in the where clause or deletes all the rows from the table if no condition is
specified. But it does not free the space containing the table.
Note: All aggregate functions described above ignore NULL values except for the
COUNT function.
A scalar function returns a single value based on the input value. Following are the
widely used SQL scalar functions:
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 23 of 49
:
ROUND() - Calculates the round-off integer value for a numeric field (or
decimal point values).
NOW() - Returns the current date & time.
FORMAT() - Sets the format to display a collection of values.
The user-defined functions in SQL are like functions in any other programming
language that accept parameters, perform complex calculations, and return a value.
They are written to use the logic repetitively whenever required. There are two types
of SQL user-defined functions:
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 24 of 49
:
36. What are the differences between OLTP and OLAP?
OLAP stands for Online Analytical Processing, a class of software programs that
are characterized by the relatively low frequency of online transactions. Queries are
often too complex and involve a bunch of aggregations. For OLAP systems, the
effectiveness measure relies highly on response time. Such systems are widely used
for data mining or maintaining aggregated, historical data, usually in multi-
dimensional schemas.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 25 of 49
:
37. What is Collation? What are the different types of Collation
Sensitivity?
Collation refers to a set of rules that determine how data is sorted and compared.
Rules defining the correct character sequence are used to sort the character data. It
incorporates options for specifying case sensitivity, accent marks, kana character
types, and character width. Below are the different types of collation sensitivity:
DELIMITER $$
CREATE PROCEDURE FetchAllStudents()
BEGIN
SELECT * FROM myDB.students;
END $$
DELIMITER ;
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 26 of 49
:
39. What is a Recursive Stored Procedure?
A stored procedure that calls itself until a boundary condition is reached, is called a
recursive stored procedure. This recursive function helps the programmers to deploy
the same set of code several times as and when required. Some SQL programming
languages limit the recursion depth to prevent an infinite loop of procedure calls
from causing a stack overflow, which slows down the system and may lead to system
crashes.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 27 of 49
:
40. How to create empty tables with the same structure as
another table?
Creating empty tables with the same structure can be done smartly by fetching the
records of one table into a new table using the INTO operator while fixing a WHERE
clause to be false for all records. Hence, SQL prepares the new table with a duplicate
structure to accept the fetched records but since no records get fetched due to the
WHERE clause in action, nothing is inserted into the new table.
SQL pattern matching provides for pattern search in data if you have no clue as to
what that word should be. This kind of SQL query uses wildcards to match a string
pattern, rather than writing the exact word. The LIKE operator is used in conjunction
with SQL Wildcards to fetch the required information.
The % wildcard matches zero or more characters of any type and can be used to
define wildcards both before and after the pattern. Search a student in your
database with first name beginning with the letter K:
SELECT *
FROM students
WHERE first_name LIKE 'K%'
Use the NOT keyword to select records that don't match the pattern. This query
returns all students whose first name does not begin with K.
SELECT *
FROM students
WHERE first_name NOT LIKE 'K%'
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 28 of 49
:
Matching a pattern anywhere using the % wildcard twice
Search for a student in the database where he/she has a K in his/her first name.
SELECT *
FROM students
WHERE first_name LIKE '%Q%'
The _ wildcard matches exactly one character of any type. It can be used in
conjunction with % wildcard. This query fetches all students with letter K at the third
position in their first name.
SELECT *
FROM students
WHERE first_name LIKE '__K%'
The _ wildcard plays an important role as a limitation when it matches exactly one
character. It limits the length and position of the matched results. For example -
PostgreSQL was first called Postgres and was developed by a team led by Computer
Science Professor Michael Stonebraker in 1986. It was developed to help developers
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 29 of 49
:
build enterprise-level applications by upholding data integrity by making systems
fault-tolerant. PostgreSQL is therefore an enterprise-level, flexible, robust, open-
source, and object-relational DBMS that supports flexible workloads along with
handling concurrent users. It has been consistently supported by the global
developer community. Due to its fault-tolerant nature, PostgreSQL has gained
widespread popularity among developers.
Indexes are the inbuilt functions in PostgreSQL which are used by the queries to
perform search more efficiently on a table in the database. Consider that you have a
table with thousands of records and you have the below query that only a few
records can satisfy the condition, then it will take a lot of time to search and return
those rows that abide by this condition as the engine has to perform the search
operation on every single to check this condition. This is undoubtedly inefficient for a
system dealing with huge data. Now if this system had an index on the column where
we are applying search, it can use an efficient method for identifying matching rows
by walking through only a few levels. This is called indexing.
This can be done by using the ALTER TABLE statement as shown below:
Syntax:
The first step of using PostgreSQL is to create a database. This is done by using the
createdb command as shown below: createdb db_name
After running the above command, if the database creation was successful, then the
below message is shown:
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 30 of 49
:
CREATE DATABASE
46. How can we start, restart and stop the PostgreSQL server?
Starting PostgreSQL: ok
Partitioned tables are logical structures that are used for dividing large tables into
smaller structures that are called partitions. This approach is used for effectively
increasing the query performance while dealing with large database tables. To create
a partition, a key called partition key which is usually a table column or an
expression, and a partitioning method needs to be defined. There are three types of
inbuilt partitioning methods provided by Postgres:
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 31 of 49
:
values. This method is most commonly used upon date fields to get monthly,
weekly or yearly data. In the case of corner cases like value belonging to the end
of the range, for example: if the range of partition 1 is 10-20 and the range of
partition 2 is 20-30, and the given value is 10, then 10 belongs to the second
partition and not the first.
List Partitioning: This method is used to partition based on a list of known
values. Most commonly used when we have a key with a categorical value. For
example, getting sales data based on regions divided as countries, cities, or
states.
Hash Partitioning: This method utilizes a hash function upon the partition key.
This is done when there are no specific requirements for data division and is
used to access data individually. For example, you want to access data based on
a specific product, then using hash partition would result in the dataset that we
require.
The type of partition key and the type of method used for partitioning determines
how positive the performance and the level of manageability of the partitioned table
are.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 32 of 49
:
We can also use the statement for removing data from multiple tables all at once by
mentioning the table names separated by comma as shown below:
TRUNCATE TABLE
table_1,
table_2,
table_3;
To get the next number 101 from the sequence, we use the nextval() method as
shown below:
SELECT nextval('serial_num');
We can also use this sequence while inserting new records using the INSERT
command:
They are character sequences bound within single quotes. These are using during
data insertion or updation to characters in the database.
There are special string constants that are quoted in dollars. Syntax:
$tag$<string_constant>$tag$ The tag in the constant is optional and when we
are not specifying the tag, the constant is called a double-dollar string literal.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 33 of 49
:
53. How can you get a list of all databases in PostgreSQL?
This can be done by using the command \l -> backslash followed by the lower-
case letter L.
This can be done by using the DROP DATABASE command as shown in the syntax
below:
If the database has been deleted successfully, then the following message would be
shown:
DROP DATABASE
ACID stands for Atomicity, Consistency, Isolation, Durability. They are database
transaction properties which are used for guaranteeing data validity in case of errors
and failures.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 34 of 49
:
The architecture of PostgreSQL follows the client-server model.
The server side comprises of background process manager, query processer,
utilities and shared memory space which work together to build PostgreSQL’s
instance that has access to the data. The client application does the task of
connecting to this instance and requests data processing to the services. The
client can either be GUI (Graphical User Interface) or a web application. The
most commonly used client for PostgreSQL is pgAdmin.
The command enable-debug is used for enabling the compilation of all libraries and
applications. When this is enabled, the system processes get hindered and generally
also increases the size of the binary file. Hence, it is not recommended to switch this
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 35 of 49
:
on in the production environment. This is most commonly used by developers to
debug the bugs in their scripts and help them spot the issues. For more information
regarding how to debug, you can refer here.
SQL standards state that the following three phenomena should be prevented whilst
concurrent transactions. SQL standards define 4 levels of transaction isolations to
deal with these phenomena.
To tackle these, there are 4 standard isolation levels defined by SQL standards. They
are as follows:
Read Uncommitted – The lowest level of the isolations. Here, the transactions
are not isolated and can read data that are not committed by other transactions
resulting in dirty reads.
Read Committed – This level ensures that the data read is committed at any
instant of read time. Hence, dirty reads are avoided here. This level makes use
of read/write lock on the current rows which prevents read/write/update/delete
of that row when the current transaction is being operated on.
Repeatable Read – The most restrictive level of isolation. This holds read and
write locks for all rows it operates on. Due to this, non-repeatable reads are
avoided as other transactions cannot read, write, update or delete the rows.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 36 of 49
:
Serializable – The highest of all isolation levels. This guarantees that the
execution is serializable where execution of any concurrent operations are
guaranteed to be appeared as executing serially.
The following table clearly explains which type of unwanted reads the levels avoid:
60. What can you tell about WAL (Write Ahead Logging)?
Write Ahead Logging is a feature that increases the database reliability by logging
changes before any changes are done to the database. This ensures that we have
enough information when a database crash occurs by helping to pinpoint to what
point the work has been complete and gives a starting point from the point where it
was discontinued.
DROP TABLE command deletes complete data from the table along with removing
the complete table structure too. In case our requirement entails just remove the
data, then we would need to recreate the table to store data in it. In such cases, it is
advised to use the TRUNCATE command.
To perform case insensitive matches using a regular expression, we can use POSIX
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 37 of 49
:
(~*) expression from pattern matching operators. For example:
'interviewbit' ~* '.*INTervIewBit.*'
We can achieve this by using the pg_dump tool for dumping all object contents in
the database into a single file. The steps are as follows:
Step 2: Execute pg_dump program to take the dump of data to a .tar folder as
shown below:
The database dump will be stored in the sample_data.tar file on the location
specified.
Parallel Queries support is a feature provided in PostgreSQL for devising query plans
capable of exploiting multiple CPU processors to execute the queries faster.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 38 of 49
:
66. Differentiate between commit and checkpoint.
The commit action ensures that the data consistency of the transaction is
maintained and it ends the current transaction in the section. Commit adds a new
record in the log that describes the COMMIT to the memory. Whereas, a checkpoint
is used for writing all changes that were committed to disk up to SCN which would
be kept in datafile headers and control files.
Conclusion:
SQL is a language for the database. It has a vast scope and robust capability of
creating and manipulating a variety of database objects using commands like
CREATE, ALTER, DROP, etc, and also in loading the database objects using
commands like INSERT. It also provides options for Data Manipulation using
commands like DELETE, TRUNCATE and also does effective retrieval of data using
cursor commands like FETCH, SELECT, etc. There are many such commands which
provide a large amount of control to the programmer to interact with the database in
an efficient way without wasting many resources. The popularity of SQL has grown
so much that almost every programmer relies on this to implement their application's
storage functionalities thereby making it an exciting language to learn. Learning this
provides the developer a benefit of understanding the data structures used for
storing the organization's data and giving an additional level of control and in-depth
understanding of the application.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 39 of 49
:
References and Resources:
PostgreSQL Download
PostgreSQL Tutorial
SQL Guide
SQL Server Interview Questions
SQL Query Interview Questions and Answers
SQL Interview Questions for Data Science
MySQL Interview Questions
DBMS Interview Questions
PL SQL Interview Questions
MongoDB Interview Questions
Database Testing Interview Questions
SQL Vs MySQL
PostgreSQL vs MySQL
Difference Between SQL and PLSQL
Difference between RDBMS and DBMS
SQL Vs NoSQL
SQL IDE
SQL Projects
MySQL Commands
SQL Books
OLTP vs OLAP
SQL MCQ
1.Which statement is true for a PRIMARY KEY constraint?
A table in SQL must have a primary key associated with it to uniquely identify its
records.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 40 of 49
:
A table in SQL is indexed by default based on its primary key.
Primary key may or may not be unique but can be comprised of multiple fields.
Foreign Key uniquely identifies all the records in the referenced table.
Foreign key may or may not be unique but can be comprised of multiple fields.
3.What is a Query?
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 41 of 49
:
Strong Question Language
MODIFY
UPDATE
ALTER TABLE
SAVE AS
Sorts the result set in descending order using the DESC keyword.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 42 of 49
:
8.SQL query used to fetch unique values from a field?
It does not require additional memory and allows for speedy retrieval of records.
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 43 of 49
:
11.An SQL query to delete a table from the database and memory while keeping the
structure of the table intact?
SELF JOIN
INNER JOIN
VIEW
NONE
13.What is the name of the component that requests data to the PostgreSQL server?
Client
Thin Client
Workstation
Interface
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 44 of 49
:
UPDATE
ADD
APPEND
INSERT
15.What is the order of results shown by default if the ASC or DESC parameter is not
specified with the ORDER BY command?
16.Which command is used to tell PostgreSQL to make all changes made to the
database permanent?
Submit
Execute
Apply
Commit
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 45 of 49
:
Super user privilege or CREATEDB privilege
Admin privilege
18.What command is used for restoring the backup of PostgreSQL which was
created using pg_dump?
19.What allows us to define how various tables are related to each other formally in a
database?
Views
Database manager
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 46 of 49
:
PL/pgSQL, PL/Tcl, PL/Perl and PL/Python
Only SQL
Blog About Us
FAQ Contact Us
Interview Preparation
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 47 of 49
:
Language, Tools & Technologies Java Interview Questions
View All
Top Articles
10 Best Data Structures And Algorithms Exciting C Projects Ideas With Source
Books Code
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 48 of 49
:
Top Cheat Sheet
Top MCQ
https://www.interviewbit.com/sql-interview-questions/amp/ 01/02/24, 5 23 PM
Page 49 of 49
: