SQL, or Structured Query Language, is a powerful and standardized programming language designed for managing and manipulating relational databases. It serves as a means to interact with databases, enabling users to create, retrieve, update, and delete data. SQL is widely used in various industries for tasks ranging from simple data queries to complex database management and administration.

Databases, organized in a tabular format, store information that can be easily accessed and managed using SQL. SQL provides a set of commands that allow users to interact with the database, making it a fundamental skill for anyone working with data or involved in software development.

These questions cover some fundamental aspects of SQL and are commonly asked in interviews to assess a candidate’s understanding of database management and SQL syntax.

Basic SQL Interview Questions 

In SQL, a primary key is a constraint that is placed on a column or a set of columns in a database table. The primary key enforces the uniqueness of values within the column(s) and ensures that each record has a unique identifier. It is specified using the “PRIMARY KEY/” clause in a table creation statement, e.g.:
CREATE TABLE table_name (
column1 data_type PRIMARY KEY,
column2 data_type,
column3 data_type,
...
);

·        TRUNCATE is a SQL command used to delete all data from a table, but unlike DELETE, it is more efficient and faster as it does not generate any undo/rollback logs. This makes it useful for large tables where the deletion of data is time-sensitive. The syntax for TRUNCATE in SQL is as follows:

TRUNCATE TABLE table_name;

·    where table_name is the name of the table from
which you want to remove all data. Note that 
TRUNCATE is a DDL (Data Definition Language) command, not a DML (Data
Manipulation Language) command. It is used to reset a table, rather than modify
its data.

In SQL, the DELETE statement is used to remove data from a table. The basic syntax of the DELETE statement is as follows:

DELETE FROM table_name [WHERE condition];

where table_name is the name of the table from which you want to delete data and condition is an optional clause to specify which rows to delete.

For example, the following DELETE statement will remove all rows from the “customers” table

DELETE FROM customers;

And this DELETE statement will remove all rows from the “customers” table where the “city” column is equal to ‘London’:

DELETE FROM customers WHERE city = 'London';
  • In SQL, the ASkeyword is used to assign an alias to a column or table name. An alias is a temporary name given to a column or table and can be used in place of the original name in SELECT, FROM, and other clauses in a SQL query

The basic syntax of using the AS keyword to assign an alias to a column is as follows:

SELECT column_name AS alias_name FROM table_name;
  • where column_nameis the name of the column you want to alias and alias_name is the new name you want to assign to the column.

The basic syntax of using the AS keyword to assign an alias to a table is as follows:

SELECT column_name(s) FROM table_name AS alias_name;
  • where table_nameis the name of the table you want to alias and alias_name is the new name you want to assign to the table.

Triggers in SQL are database objects that automatically activate (or “trigger”) in response to specific events such as INSERT, UPDATE, or DELETE statements being executed on a specified table. They allow you to perform custom actions such as:

  • Validate data before changes are made
  • Automatically update related records
  • Enforce business rules or data integrity
  • Log changes made to the data.

Triggers are executed in the context of a transaction, so either all of the actions performed by the trigger are committed, or none of them are.

  • Normalization in SQL refers to the process of organizing a database to minimize redundancy and dependency. This is done by dividing large tables into smaller, more manageable tables and defining relationships between them. The goal is to ensure data integrity and improve performance. The process of normalization follows a set of guidelines called normal forms (1NF, 2NF, 3NF, etc.) that dictate how data should be organized.
  • SQL (Structured Query Language) is a standard programming language used for managing and manipulating relational databases. It is used to insert, update, and retrieve data from a database, as well as to create and modify database structures such as tables, indexes, and relationships. Some common operations performed using SQL include creating tables, inserting data into tables, updating data in tables, and retrieving data from tables using SELECT statements. SQL is widely used in various industries, including finance, healthcare, e-commerce, and government.



  • A query in SQL is a request to retrieve data from a database. It is used to extract specific information from one or more tables based on specified conditions. A query typically includes a SELECT statement that defines the columns to be returned, a FROM clause that specifies the table(s) the data is being retrieved from, and a WHERE clause that defines the conditions that must be met for a row to be included in the results. Queries can also include clauses for grouping, ordering, and limiting the results. The results of a query are returned in the form of a table and can be used for further analysis or manipulation.

DDL (Data Definition Language) is a subset of SQL used for managing the structure of a database, including creating, modifying, and deleting tables, indexes, and other database objects. DDL statements are used to define the schema of a database and establish the relationships between tables.

Some common DDL statements include:

  • CREATE TABLE:used to create a new table in the database.
  • ALTER TABLE:used to modify an existing table, such as adding or dropping columns.
  • DROP TABLE:used to delete an existing table.
  • CREATE INDEX:used to create an index on one or more columns in a table to improve query performance.
  • DROP INDEX:used to delete an existing index.
  • CREATE VIEW:used to create a virtual table based on the result of a SELECT statement.
  • DROP VIEW:used to delete an existing view.

DDL statements are typically executed by a database administrator and are used to define the structure of a database. They are not used to manipulate data in the database.

DML (Data Manipulation Language) is a subset of SQL used for managing data stored in a database, including inserting, updating, and deleting data. DML statements are used to manipulate data in a database and are typically executed by an application or end-user.

Some common DML statements include:

  • SELECT:used to retrieve data from one or more tables.
  • INSERT:used to add new rows to a table.
  • UPDATE:used to modify existing data in a table.
  • DELETE:used to remove rows from a table.
  • MERGE:used to combine the contents of two tables into a single table.

DML statements are used to manipulate data in a database and are essential for most database-driven applications. They are used to perform operations such as retrieving data for display, updating data based on user input, and removing data that is no longer needed.



To create a database in SQL, you need to use the CREATE DATABASE statement. The basic syntax is as follows:

CREATE DATABASE database_name;
  1. CREATE DATABASE :
  • Specifies that a new database is being created.
  1. database_name :
  • Specifies the name of the new database.

Here is an example that creates a database named mydatabase:

CREATE DATABASE mydatabase;

Once you have created the database, you can then create tables, indexes, and other database objects within it. Note that the specific syntax for creating a database can vary between different SQL implementations, such as MySQL, Oracle, Microsoft SQL Server, and others.

To create a table in SQL, you need to use the CREATE TABLE statement. The basic syntax is as follows:

CREATE TABLE table_name (
column1 data_type,
column2 data_type,
...
);
  1. CREATE TABLE :
  • Specifies that a new table is being created.
  1. table_name :
  • Specifies the name of the new table.
  1. column1, column2, etc. :
  • Specify the names of the columns in the table.
  1. data_type :
  • Specifies the data type of each column.

Here is an example that creates a table named employees with columns for the employee’s ID, name, and salary:

CREATE TABLE employees (
employee_id INT PRIMARY KEY,
last_name VARCHAR(30),
first_name VARCHAR(30),
salary DECIMAL(8,2)
);

In this example, the employee_id column is defined as the primary key of the table, which is a unique identifier for each row. The last_name and first_name columns are character strings of up to 30 characters, and the salary column is a decimal number with 8 digits and 2 decimal places.

Note that the specific syntax for creating a table can vary between different SQL implementations, such as MySQL, Oracle, Microsoft SQL Server, and others.



There are four main types of JOINs in SQL:

  1. INNER JOIN :
  • Returns only the rows that have matching values in both tables. This is the most commonly used type of join.
  1. LEFT JOIN (or LEFT OUTER JOIN) :
  • Returns all the rows from the left table (table1), and the matching rows from the right table (table2). The result will contain NULL values for non-matching rows from the right side.
  1. RIGHT JOIN (or RIGHT OUTER JOIN) :
  • Returns all the rows from the right table (table2), and the matching rows from the left table (table1). The result will contain NULL values for non-matching rows from the left side.
  1. FULL OUTER JOIN :
  • Returns all rows from both tables, with NULL values for non-matching rows.

The syntax for joining two tables varies between different SQL implementations, but the basic structure is as follows:

SELECT column1, column2, ... FROM table1 JOIN table2 ON table1.column = table2.column;
  • SELECT:Specifies the columns that you want to retrieve from the tables.
  • FROM:Specifies the first table to be joined.
  • JOIN:Specifies the type of join you want to perform.
  • ON:Specifies the join condition, which determines how the rows from the two tables are matched. The join condition is usually specified using the ON keyword, followed by an expression that evaluates to a Boolean value.

Note that the specific syntax for joining tables can vary between different SQL implementations, such as MySQL, Oracle, Microsoft SQL Server, and others.



The SELECT statement in SQL is used to retrieve data from a database. The basic syntax is as follows:

SELECT column1, column2, ... FROM table_name;
  • SELECT:Specifies the columns that you want to retrieve from the table.
  • column1, column2, …:Specifies the columns that you want to retrieve. You can use the * wildcard character to retrieve all columns.
  • FROM:Specifies the name of the table.
  • table_name:Specifies the name of the table that you want to retrieve data from.

Here is an example that retrieves the last name and salary of all employees in the employees table:

SELECT last_name, salary FROM employees;
  • In this example, the SELECTstatement retrieves the last_name and salary columns from the employees The results will show the last name and salary for all employees in the employees table.
  • Note that the specific syntax for the SELECTstatement can vary between different SQL implementations, such as MySQL, Oracle, Microsoft SQL Server, and others.



SQL (Structured Query Language) is a standardized programming language used to manage relational databases. Here are some of the most important and commonly used SQL commands:

  • SELECT– used to retrieve data from one or more tables
  • INSERT– used to add new data to a table
  • UPDATE– used to modify existing data in a table
  • DELETE– used to remove data from a table
  • CREATE– used to create a new database, table, or other database object
  • DROP– used to delete a database, table, or other database object
  • ALTER– used to modify the structure of a table
  • JOIN– used to combine data from two or more tables based on a related column
  • GROUP BY– used to group data based on one or more columns
  • ORDER BY– used to sort data based on one or more columns
  • WHERE– used to filter data based on a condition
  • HAVING– used to filter data based on a condition involving an aggregate function
  • UNION– used to combine the results of two or more SELECT statements
  • DISTINCT– used to retrieve unique values from a column
  • LIKE– used to search for a pattern in a column
  • IN– used to filter data based on a list of values
  • COUNT– used to count the number of rows that match a specific condition
  • MAX– used to retrieve the maximum value from a column
  • MIN– used to retrieve the minimum value from a column
  • AVG– used to retrieve the average value from a column.

SQL (Structured Query Language) is used for:

  • Managing and manipulating relational databases.
  • Creating, altering and deleting tables and other database objects.
  • Inserting, updating and retrieving data from databases.
  • Running queries to extract and analyze data from databases.
  • Controlling access to the data and maintaining data integrity.
  • Creating stored procedures, functions and triggers.
  • Implementing data security through roles, permissions and encryption.
  • Backup and recovery of databases.

SQL constraints are rules that are used to enforce the integrity of data in a database. There are several types of constraints in SQL, including:

  • NOT NULL– ensures that a column cannot have a NULL value.
  • UNIQUE– ensures that the values in a column are unique across all rows.
  • PRIMARY KEY– creates a unique identifier for each row in a table and enforces the NOT NULL and UNIQUE constraints.
  • FOREIGN KEY– creates a relationship between two tables and ensures that data in one table references data in another table.
  • CHECK– restricts the values that can be inserted into a column to those that satisfy a specified condition.
  • DEFAULT– provides a default value for a column if no value is specified during insertion.
  • INDEX– helps to improve the performance of queries by allowing for quick retrieval of data based on the values in specified columns.
  • A self-join in SQL involves joining a table with itself. This is useful when you have a table that contains hierarchical or recursive data, such as a table of employees where each employee has a manager who is also an employee.
  • To perform a self-join, you need to alias the table with different names. Here’s an example SQL query that demonstrates a self-join:
SELECT e1.name AS employee, e2.name AS manager
FROM employees e1
INNER JOIN employees e2 ON e1.manager_id = e2.employee_id
  • In this example, we have a table called “employees” that has columns for employee_id, name, and manager_id. The manager_id column contains the employee_id of the employee’s manager.
  • To join the table with itself, we use the aliases “e1” and “e2” to differentiate between the two instances of the employees table. The join condition is based on the manager_id column, which is used to match each employee with their manager.
  • The result of this query will be a table that lists each employee along with their manager’s name.
  • When you “left join” in SQL, you are combining the records of two tables, but the records of the left-side (or first) table are included in the result set, even if there is no match in the right-side (or second) table. The result will contain all rows from the left-side table and the matching rows from the right-side table, with NULL values for the non-matching rows of the right-side table.
  • When you “left join” is omitted, you will get a “cross join” instead. A cross join returns the Cartesian product of two tables, meaning every row from the first table is combined with every row from the second table. The result set will contain every possible combination of rows from the two tables.
  • For example, if you have two tables, A and B, with 100 and 200 records respectively, the result of a cross join between the two tables will contain 20,000 records (100 * 200). In most cases, cross joins are not what you want, as the result set can be very large and not meaningful.
  • In SQL, a variable is used to store a value that can be reused throughout a SQL script or within a single SQL statement. Variables are declared and assigned a value using the following syntax:
DECLARE @variable_name data_type = value;
  • Where variable_nameis the name of the variable, data_type is the data type of the value that the variable will hold, and value is the value that will be stored in the variable.

For example:

DECLARE @counter INT = 0;
  • This declares a variable named @counterof data type INT and assigns it an initial value of 0. The value of a variable can be updated within the same script or statement by using the following syntax:
SET @variable_name = new_value;
  • Where variable_nameis the name of the variable, and new_value is the new value that will be assigned to the variable.
  • In SQL, variables can be used to simplify complex calculations, store intermediate results, and control the flow of execution in a SQL script.



SQL has several data types, including:

  • Numeric data types: INT, BIGINT, SMALLINT, TINYINT, DECIMAL, NUMERIC, FLOAT, and REAL
  • Character and string data types: CHAR, VARCHAR, NVARCHAR, TEXT, and NTEXT
  • Binary data types: BINARY and VARBINARY
  • Date and time data types: DATE, DATETIME, SMALLDATETIME, TIME, and DATETIME2
  • Boolean data type: BIT
  • Unique identifier data type: UNIQUEIDENTIFIER
  • Spatial data types: GEOMETRY and GEOGRAPHY

The exact data types available in SQL may vary slightly depending on the specific implementation (e.g. Microsoft SQL Server, MySQL, PostgreSQL, etc.).

The difference between VARCHAR and NVARCHAR data types in SQL is the way they store character data:

  1. VARCHAR :
  • Stands for “variable-length character string”. It stores characters in single-byte encoding such as ASCII or ISO-8859-1.
  1. NVARCHAR :
  • Stands for “Unicode variable-length character string”. It stores characters in double-byte encoding such as Unicode.

Therefore, NVARCHAR is capable of storing a wider range of characters compared to VARCHAR and is suitable for storing international characters. However, NVARCHAR requires twice the storage space as VARCHAR for the same data.

An index in SQL is a data structure that provides faster access to data stored in a database table. It works like an index in a book, allowing quick access to the information you need without having to scan the entire table.

There are several types of indexes in SQL, including:

  1. Clustered Index :
  • It physically reorders the rows in the table based on the column specified in the index. Only one clustered index can be created for a table.
  1. Non-Clustered Index :
  • It contains a copy of the indexed columns and a pointer to the corresponding row in the table. Multiple non-clustered indexes can be created for a table.
  1. Composite Index :
  • It contains multiple columns in the index, allowing for more efficient queries that use multiple conditions.
  1. Unique Index :
  • It ensures that the index key values are unique across all rows in the table.
  1. Bitmap Index :
  • It uses a bitmap to represent the data, where each bit in the map represents the presence or absence of a certain value in the indexed columns.
  1. Spatial Index :
  • It is used to optimize the performance of spatial data queries, such as those involving geography or geometry data types.

The type of index to use depends on the specific requirements and usage patterns of the data. The correct choice of index can greatly improve the performance of database queries.



  • PostgreSQL, commonly known as “Postgres,” is an open-source relational database management system (DBMS) that is used for a wide range of applications. It was first developed in 1986 by the University of California, Berkeley and has since become one of the most popular and widely used relational database systems in the world.
  • PostgreSQL is known for its strong support for SQL, which is the standard language for relational databases, as well as for its support for advanced features such as transactions, stored procedures, and user-defined types. It also supports NoSQL features such as JSON and hstore for handling unstructured data.
  • In addition to its feature set, PostgreSQL is known for its reliability, scalability, and security. It has been designed to handle large amounts of data and can be used for both small and large applications. It also has a large and active community of developers and users, who contribute to its development and provide support.
  • PostgreSQL is used in many industries, including finance, healthcare, education, and e-commerce. It is also used by many organizations, both large and small, as their primary relational database management system.



TCL in SQL stands for Transaction Control Language. It is a set of commands used to manage transactions in a relational database management system. TCL commands include:

  • COMMIT– save changes made in the transaction to the database
  • ROLLBACK– undo changes made in the transaction
  • SAVEPOINT– set a savepoint within a transaction to allow rolling back to a certain point
  • SET TRANSACTION– start a transaction and set transaction properties.

TCL is used to ensure data integrity and consistency by grouping related SQL statements into a single transaction, which either all succeed or all fail as a unit.

DQL stands for Data Query Language. In SQL (Structured Query Language), DQL refers to the subset of SQL commands used to retrieve data from a database, without modifying the data stored in the database.

Some common DQL commands in SQL include:

  • SELECT– used to retrieve data from one or more tables in a database
  • FROM– specifies the table(s) from which to retrieve data
  • WHERE– used to filter data based on specified conditions
  • GROUP BY– used to group data based on one or more columns
  • HAVING– used to filter groups based on aggregate values
  • ORDER BY– used to sort data in ascending or descending order

DCL in SQL stands for Data Control Language. It is a set of commands used to control access to data stored in a database. DCL commands include:

  • GRANT– give specific permissions to users or roles to access or manipulate data in the database
  • REVOKE– remove previously granted permissions from users or roles

DCL is used to maintain the security and integrity of data stored in a database by regulating who can perform specific actions on the data, such as reading, writing, or deleting data. This helps to ensure that sensitive information is protected and that unauthorized users do not make changes to the data that could result in data loss or corruption.

 

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Categorized in: