SQL Basic Questions

·

5 min read

Hey Reader, hope you are doing good. In this article let us learn most asked SQL questions in interviews. We will be learning from basic to advanced level of understanding in the article.

In this article let us understand

  1. What is SQL

  2. What is the difference between SQL and NoSQL

  3. What are Stored Procedures in SQL

  4. What is the use of SQL Indexes

  5. Types of Indexes

  6. What are SQL Joins

  7. Types of SQL Joins

Lets begin with the first topic:

  1. What is SQL?
  • SQL stands for Structured Query Language.

  • It is a programming language used to manage and manipulate relational databases. It is not a database but a language.

  • We can do querying, updating and managing data in relational databases.

  • It follows a strict schema (syntax) and supports CRUD operations (Create, Read, Update, Delete).

  • The common SQL commands includes SELECT, INSERT, UPDATE, and DELETE.

2. What is the difference between SQL and NoSQL ?

3. What are Stored Procedures in SQL ?

  • A Stored Procedure is a precompiled set of SQL statements that are stored in a database and executed in a single set.

  • It is similar to a function/method in a programming but it is written in SQL and runs within the database servers.

  • Once the Stored Procedures are created it can be executed multiple times with different parameters.

  • Stored Procedures improves performance compared to executing raw queries repeatedly.

  • Stored Procedures can also accept input parameters and return them vice-versa.

  • Complex operations involving multiple SQL statements can be encapsulated within a single procedure.

  • Syntax to write Stored Procedure

CREATE PROCEDURE ProcedureName
    @ParameterName DataType,
    @ParameterName2 DataType OUTPUT
AS
BEGIN
    -- SQL statements
    SELECT * FROM TableName WHERE ColumnName = @ParameterName;

    -- Optionally return data
    SET @ParameterName2 = (SELECT COUNT(*) FROM TableName);
END;

4. What is the use of SQL Indexes?

  • An SQL index is a database object that improves the speed of data retrieval operations on a table.

  • It works like an index in a books, helping the database queries to locate the data quickly without scanning the entire table.

  • It speeds ups the SELECT statements by reducing number of rows the database must scan.

  • Helps in sorting data (ORDER BY) and filtering rows (WHERE clause).

  • Helps in Join Operations by indexing the join column.

  • Unique Indexes ensures that no duplicate values exist in a column (used internally for PRIMARY KEY and UNIQUE constraints)

Types Of Indexes

  1. Primary Index
  • Automatically created when defining a PRIMARY KEY

  • Ensures unique values for the Primary Key Column

Example:

CREATE TABLE Employees (
    EmployeeID INT NOT NULL,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    PRIMARY KEY (EmployeeID)
);

2. Unique Index

  • Ensures all the values in the indexed column(s) are unique.
Example:

CREATE TABLE Employees (
    EmployeeID INT NOT NULL,
    Email VARCHAR(100),
    UNIQUE (Email)
);

3. Clustered Index

  • It reorders the table rows to match the index order.

  • Only one clustered index per table (typically created on the primary key).

  • Clustered Index Supported in some databases like SQL Server; by default, the primary key creates a clustered index

CREATE TABLE Orders (
    OrderID INT NOT NULL,
    CustomerID INT,
    OrderDate DATE,
    PRIMARY KEY CLUSTERED (OrderID)
);

4. Non-Clustered Index

  • Maintains a logical order of data with pointers to the actual rows.

  • A table can have multiple non-clustered indexes.

CREATE TABLE Products (
    ProductID INT NOT NULL,
    ProductName VARCHAR(100),
    CategoryID INT,
    Price DECIMAL(10, 2),
    INDEX (CategoryID)
);

5. Composite Index

  • Index on multiple columns
CREATE INDEX idx_composite
ON Orders (CustomerID, OrderDate);

6. Full-Text Index

  • Used for complex text searches (e.g., searching specific phrases or words in a column)

5. What are SQL Joins?

  • SQL joins are used to combine rows from two or more tables based on a related column between them.

  • It allows you to query and retrieve data that is stored across multiple tables in a relational database.

Types of Joins in SQL

  1. Inner Join
  • Returns only the rows where there is a match in both tables.
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
INNER JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;

2. LEFT JOIN

  • Returns all rows from the left table and the matching rows from the right table. If no match is found, NULL is returned.
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;

3. RIGHT JOIN

  • Returns all rows from the right table and the matching rows from the left table. If no match is found, NULL is returned.
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;

4. FULL JOIN

  • Returns all rows from both tables, with NULL in columns where no match is found.
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
FULL JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;

5. CROSS JOIN

  • Returns the Cartesian product of two tables (every combination of rows).
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
CROSS JOIN Departments;

6. SELF JOIN

  • A table joins itself; useful for hierarchical data.
SELECT A.EmployeeID AS ManagerID, B.Name AS EmployeeName
FROM Employees A
INNER JOIN Employees B
ON A.EmployeeID = B.ManagerID;

I hope you found this article insightful and valuable. Your support means a lot to me — please feel free to share your thoughts, feedback, or suggestions in the comments.

If you enjoyed reading, don’t forget to clap, comment and share it with others who might benefit. Your encouragement inspires me to create more such content. Thank you for your time!!!