Top-30 SQL questions in 2020 interviews that you should know
Relational databases are among the most frequently used databases to date, and therefore SQL skills are mandatory for most positions.
In this article with SQL interview questions I will introduce you to the most frequently asked questions on SQL (Structured Query Language).
Our SQL Queries article is a universal resource where you can accelerate your interview preparation. It consists of a set of 30 most common questions that the interviewer can ask during the interview.
It usually starts with basic SQL questions and then moves on to more complex questions based on the discussion and your answers. These SQL questions from the interviews will help you get the most out of it at different levels of understanding.
Let’s get started!
WHAT IS THE DIFFERENCE BETWEEN DELETE AND TRUNCATE OPERATORS?
DELETE: Used to delete a row in the table, You can restore data after deletion, DML command, Slower than the TRUNCATE operator.
TRUNCATE: Used to delete a row in the table, You cannot restore data (note: operations are logged differently, but SQL Server has an option to rollback), DDL command, Hurry up.
WHAT ARE THE SUBSETS OF SQL?
DDL (Data Definition Language) — allows you to perform various operations with the database, such as CREATE (creation), ALTER (change) and DROP (delete objects).
DML (Data Manipulation Language) — allows you to access and manipulate data, for example, insert, update, delete and extract data from the database.
DCL (Data Control Language) — allows you to control access to the database. Example — GRANT (grant rights), REVOKE (revoke rights).
WHAT IS MEANT BY DBMS? WHAT TYPES OF DBMS DO EXIST?
Database — a structured collection of data. Database Management System (DMS) — software that interacts with the user, applications and the database itself for data collection and analysis. DBMS allows the user to interact with the database. The data stored in the database can be modified, retrieved and deleted. They can be of any type, such as strings, numbers, images, etc.
There are two types of DBMS:
- Relational Database Management System: Data is stored in relationships (tables). An example is MySQL.
- Non-relational Database Management System: There is no concept of relations, tuples or attributes. Example — Mongo.
WHAT IS MEANT BY TABLE AND FIELD IN SQL?
Table is an organized set of data in the form of rows and columns. The field is the columns in the table. For example:
Field: Stu_Id, Stu_Name, Stu_Marks
WHAT ARE SQL CONNECTIONS?
The JOIN operator is used to connect rows from two or more tables based on the column linked between them. It is used to combine two tables or get data from them. There are 4 types of connections in SQL, namely:
- Inner Join
- Right Join
- Left Join
- Full Join
WHAT IS THE DIFFERENCE BETWEEN THE CHAR AND VARCHAR DATA TYPE IN SQL?
Both Char and Varchar are character data types, but varchar is used for variable length character strings, while Char is used for fixed length strings.
For example, Char(10) can only store 10 characters and cannot store a string of any other length, while varchar(10) can store a string of any length up to 10, i.e. 6, 8 or 2.
WHAT IS A PRIMARY KEY?
- Primary key — a column or set of columns that uniquely identifies each row in the table.
- Unambiguously identifies one row in the table.
- Zero (Null) values are not allowed.
Example: In the Student table, the Stu_ID is the primary key.
WHAT ARE CONSTRAINTS?
Restrictions (Constraints) are used to specify a restriction on the table data type. They can be specified when creating or changing a table. Example of a constraint:
- NOT NULL
- PRIMARY KEY
- FOREIGN KEY
WHAT IS THE DIFFERENCE BETWEEN SQL AND MYSQL?
SQL is a standard language for Structured Query Language based on English, while MySQL is a database management system. SQL is a relational database language used for data access and management, whereas MySQL is a relational DBMS (Database Management System), as well as SQL Server, Informix, etc.
WHAT IS A UNIQUE KEY?
- Unambiguously identifies one row in the table.
- Many unique keys in one table are acceptable.
- NULL values are allowed (note: it depends on the DBMS, in SQL Server a NULL value can be added only once in the UNIQUE KEY field).
WHAT IS A FOREIGN KEY?
- A foreign key supports referential integrity by providing communication between the data in two tables.
- The foreign key in the child table refers to the primary key in the parent table.
- Restricting the foreign key prevents actions that break the connection between the child and parent tables.
WHAT IS MEANT BY DATA INTEGRITY?
Data integrity defines the accuracy as well as consistency of data stored in a database. It also defines integrity restrictions to ensure compliance with business rules for data when it is entered into an application or database.
WHAT IS THE DIFFERENCE BETWEEN CLUSTERED AND NON-CLUSTERED INDICES IN SQL?
The difference between clustered and non-clustered indexes in SQL:
- A cluster index is used to easily and quickly extract data from a database, while reading from an unclassified index is relatively slower.
- Clustered index changes the way the records are stored in the database — it sorts the rows by column, which is set as a clustered index, while in non-clustered index it does not change the way of storage, but creates a separate object inside the table, which points to the source rows of the table when searching.
- One table can have only one clustered index, while it can have many unclustered indexes.
WRITE AN SQL QUERY TO DISPLAY THE CURRENT DATE
SQL has a built-in GetDate function () that helps you return the current timestamp/date.
LIST THE CONNECTION TYPES
There are different types of connections that are used to extract data between tables. Basically they are divided into four types, namely:
Inner Join: In MySQL, this is the most common type. It is used to return all rows from multiple tables for which a connection condition is met.
Left Join: In MySQL, it returns all rows from the left (first) table and only the matching rows from the right (second) table for which the connection condition is met.
Right Join: in MySQL, it returns all rows from the right (second) table and only the matching rows from the left (first) table for which the connection condition is executed.
Full Join: returns all records that have a match in any of the tables. Consequently, it returns all rows from the left table and all rows from the right table.
WHAT DO YOU MEAN BY DENORMALIZATION?
Denormalization is a technique that is used to transform from higher to lower normal forms. It helps database developers improve the performance of the entire infrastructure by adding redundancy to the table. It adds redundancy to a table, given the frequent database queries that combine data from different tables into a single table.
WHAT ARE ENTITIES AND RELATIONSHIPS?
Essences: A person, place, or object in the real world whose data can be stored in a database. Tables contain data that represent the same entity type. For example — a bank database has a table of clients to store information about clients. The table of clients stores this information as a set of attributes (columns in the table) for each client.
Relationships: relations or connections between entities that have some kind of relation to each other. For example — the client’s name is associated with the client’s account number and contact information, which may be in the same table. There may also be relationships between separate tables (e.g. client to account).
WHAT IS AN INDEX?
Indexes refer to a performance tuning method that allows you to extract records from a table more quickly. An index creates a separate structure for the field to be indexed and therefore allows faster retrieval of data.
DESCRIBE THE DIFFERENT TYPES OF INDEXES
There are three types of indexes, namely:
- Unique Index: This index does not allow a field to have repetitive values if the column is indexed uniquely. If the primary key is defined, the unique index can be applied automatically.
- Clustered Index: This index changes the physical order of the table and performs a search based on the key values. Each table can have only one Clustered Index.
- Non-Clustered Index: This index does not change the physical order of the table and maintains the logical data order. Each table can have many unclustered indexes.
WHAT IS NORMALIZATION AND WHAT ARE ITS ADVANTAGES?
Normalization is the process of data organization, which aims to avoid duplication and redundancy. Some of its advantages:
- Better database organization
- More tables with small rows
- Effective access to data
- More flexibility for requests
- Quick search for information
- It’s easier to implement data security
- Allows for easy modification
- Reducing redundancy and duplication of data
- Smaller database
- Provides consistency of data after changes are made
WHAT IS THE DIFFERENCE BETWEEN DROP AND TRUNCATE COMMANDS?
The DROP command deletes the table itself, and you can’t make a Rollback command, while the TRUNCATE command deletes all rows from the table (note: SQL Server Rollback will work fine and roll back DROP).
EXPLAIN THE DIFFERENT TYPES OF NORMALIZATION
There are many consecutive normalization levels. These are the so-called normal forms. Each subsequent normal form includes the previous one. The first three normal forms are usually enough.
- The first normal form (1NF) — there are no repetitive groups in the lines
- The second normal form (2NF) — each non-key (supporting) column value depends on the whole primary key
- Third normal form (3NF) — each non-key value depends only on the primary key and does not depend on another non-key column value
WHAT IS THE ACID PROPERTY IN THE DATABASE?
ACID means Atomicity, Consistency, Isolation, Durability. It is used to provide reliable processing of data transactions in a database system.
- Atomicity. Guarantees that the transaction will be fully executed or fail, where the transaction represents a single logical data transaction. This means that if one part of any transaction fails, the entire transaction fails and the database state remains unchanged.
- Consistency. Guarantees that the data must comply with all validation rules. Simply put, you can say that your transaction will never leave your database in an unacceptable state.
- Isolation. The main purpose of isolation is to control the mechanism of parallel data change.
- Longevity. Longevity means that if a transaction is validated (COMMIT), the changes that occur within the transaction will be preserved regardless of what may get in their way (e.g. power loss, failure or errors of any kind).
WHAT DO YOU MEAN BY “TRIGGER” IN SQL?
An SQL Trigger is a special type of stored procedures that are designed for automatic execution at the moment or after a data change. This allows you to execute a code packet when an insert, update, or any other query is executed to a specific table.
WHICH OPERATORS ARE AVAILABLE IN SQL?
There are three types of operators available in SQL, namely:
- Arithmetic Operators
- Logical Operators
- Comparator operators
DO THE VALUES OF NULL MATCH THE VALUES OF ZERO OR SPACE?
The value of NULL is not equal to zero or space. A value of NULL represents a value that is unavailable, unknown, assigned or inapplicable, while zero is a number and the space is a character.
WHAT IS THE DIFFERENCE BETWEEN A CROSS JOIN AND A NATURAL JOIN?
A cross join creates a cross or Cartesian product of two tables, whereas a natural join is based on all columns having the same name and data types in both tables.
WHAT IS A SUBQUERY IN SQL?
A subquery is a query within another query that defines a query for extracting data or information from a database. In a subquery, an external query is called the main query, while an internal query is called a subquery. Subqueries are always executed first, and the result of the subquery is passed on to the main query. It can be nested in SELECT, UPDATE or any other query. The subquery can also use any comparison operator, such as >, < or =.
WHAT ARE THE TYPES OF SUBQUERIES?
There are two types of subqueries, namely: correlated and uncorrelated.
- A correlated subquery is a query that selects data from a table with a reference to an external query. It is not considered an independent query because it refers to another table or column in the table.
- Uncorrected subquery: This query is an independent query in which the output data of the subquery is substituted for the main query.
LIST THE WAYS TO GET THE NUMBER OF RECORDS IN A TABLE?
To calculate the number of records in a table, you can use the following commands:
SELECT * FROM table1
SELECT COUNT(*) FROM table1
SELECT rows FROM sysindexes WHERE id = OBJECT_ID(table1) AND indid < 2