0% found this document useful (0 votes)
28 views19 pages

DBMS vs RDBMS and SQL Constraints Explained

The document outlines key concepts in SQL, including differences between DBMS and RDBMS, types of SQL constraints, triggers, joins, and indexing. It explains the functionality of primary and unique keys, the use of subqueries, and the distinctions between clustered and non-clustered indexes. Additionally, it covers the differences between stored procedures and functions, providing examples and explanations for better understanding.

Uploaded by

mallikarjun5813
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views19 pages

DBMS vs RDBMS and SQL Constraints Explained

The document outlines key concepts in SQL, including differences between DBMS and RDBMS, types of SQL constraints, triggers, joins, and indexing. It explains the functionality of primary and unique keys, the use of subqueries, and the distinctions between clustered and non-clustered indexes. Additionally, it covers the differences between stored procedures and functions, providing examples and explanations for better understanding.

Uploaded by

mallikarjun5813
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1) What is the difference between DBMS and RDBMS ?

(basic question)

DBMS RDBMS

Stands for Relational Database


Stands for Database Management System
Management System

Stores data as files or hierarchical/network


Stores data in tables (rows & columns)
structures

Relationships between tables using


No concept of relationships between data
foreign keys

Does not enforce ACID properties strictly Strictly follows ACID properties

Supports normalization to reduce


No normalization support or very limited
redundancy

Handles large amounts of data


Used for small amounts of data
efficiently

Examples: SQL Server, MySQL, Oracle,


Examples: File System, XML DB, dBase
PostgreSQL

Security is basic High-level security (roles, permissions)

2) What is a Constraint is SQL? What are its types? (very important question)

A constraint in SQL is a rule applied to a table’s column(s) to enforce data integrity,


accuracy, and validity of the data stored in the database.

Constraints ensure that only valid and consistent data is inserted, updated, or deleted.

Types of Constraints in SQL:

 NOT NULL: Ensures a column cannot have NULL (empty) values.


 UNIQUE: Ensures all values in a column or a set of columns are distinct; no
duplicates allowed. One Null allowed
 PRIMARY KEY: A combination of NOT NULL and UNIQUE; uniquely identifies
each row in a table.
 FOREIGN KEY: Maintains referential integrity by ensuring that a value in one table
matches a value in another table's primary key.
 CHECK: Enforces domain integrity by limiting the values that can be placed in a
column based on a condition (e.g., salary > 0).
 DEFAULT: Sets a default value for a column if no value is provided during insertion.
Example:-

CREATE TABLE Employees (

EmpId INT NOT NULL, //PRIMARY KEY Constraint

EmpCode VARCHAR(20) NOT NULL, //UNIQUE or NOT NULL Constraint

EmpName VARCHAR(100) NOT NULL, //NOT NULL Constraint

Email VARCHAR(100),

Age INT, //CHECK Constraint

Salary DECIMAL(10,2) DEFAULT 25000, //Default Constraint

DeptId INT, //FOREIGN KEY Constrain

-- PRIMARY KEY Constraint

CONSTRAINT PK_Employees PRIMARY KEY (EmpId),

-- UNIQUE Constraint

CONSTRAINT UQ_Employees_EmpCode UNIQUE (EmpCode),

-- CHECK Constraint

CONSTRAINT CHK_Employees_Age CHECK (Age >= 18),

-- FOREIGN KEY Constraint

CONSTRAINT FK_Employees_Departments FOREIGN KEY (DeptId)

REFERENCES Departments(DeptId),

-- NOT NULL is applied inline above (EmpCode, EmpName)

-- DEFAULT Constraint is inline above (Salary)


);

3) What is the difference between Primary key and Unique key? (important question)

Primary Key uniquely identifies each record and cannot contain NULLs,
while a Unique Key enforces uniqueness but allows one NULL and multiple unique keys per table.

Feature Primary Key Unique Key

Ensures all values are


Purpose Uniquely identifies each row
unique

NULL allowed? ❌ No NULL allowed ✔ Allows one NULL

How many per ✔ Can have multiple


❌ Only one Primary Key
table? Unique Keys

Creates a Clustered Index Creates a Non-Clustered


Index created?
(by default) Index

Uniqueness Mandatory Optional

Used for Yes, referenced by Foreign


Rarely used for that
relationships? Key

Column Can be composite (multi-


Can also be composite
combination? column)

4) What are Triggers and types of triggers? (very important question Ask in tcs, wipro,
Infosys, Accenture etc)

A Trigger is a special kind of stored procedure that automatically executes in


response to specific events in a database.

It is fired automatically, so you cannot call a trigger manually.

Triggers are useful for:

 Maintaining audit logs


 Enforcing business rules
 Auto-updating related tables
 Preventing invalid operations

The main types of triggers are:

1. Data Manipulation Language (DML) Triggers:


These fire in response to data changes in a table, such as INSERT, UPDATE,
or DELETE operations.
 Subtypes include INSTEAD OF triggers (which execute before the triggering
action and can replace it) and AFTER triggers (which execute after the
triggering action completes).
2. Data Definition Language (DDL) Triggers:
 These activate in response to changes in the database schema, such as creating,
altering, or dropping tables.
 They are useful for auditing or preventing schema modifications.
3. Logon Triggers:
 These fire in response to user logon events to a database server, before the
user's session begins.
 They can be used to control or audit login activities.

5) What is the difference between Having clause and Where clause? (very important
question Ask in tcs, wipro, Infosys, Accenture etc)

The WHERE clause filters rows before any grouping or aggregation happens in SQL. It is used
to filter individual rows from a table based on specified conditions and cannot be used with
aggregate functions. It can be used with SELECT, UPDATE, and DELETE statements.

The HAVING clause, on the other hand, filters groups after aggregation. It operates on
grouped data created by the GROUP BY clause and can include conditions involving
aggregate functions like SUM, AVG, or COUNT. HAVING is used only with SELECT statements.
Key Differences:
 WHERE filters rows before grouping; HAVING filters groups after aggregation.
 WHERE cannot use aggregate functions; HAVING can.
 WHERE can be used without GROUP BY; HAVING requires GROUP BY.
 WHERE is generally more efficient because it filters data at an earlier stage in the query.
 WHERE works with individual rows; HAVING works with groups or aggregated results.

In short, use WHERE to filter raw data rows and HAVING to filter aggregated data groups in
SQL queries.

✅ Example to Understand Easily


1️⃣ WHERE filters before grouping
SELECT Department, COUNT(*)
FROM Employees
WHERE Salary > 50000 -- filters rows
GROUP BY Department;
Here:
 Only employees with salary > 50,000 are grouped.

2️⃣ HAVING filters after grouping


SELECT Department, COUNT(*)
FROM Employees
GROUP BY Department
HAVING COUNT(*) > 5; -- filters groups
Here:
 Only departments with more than 5 employees appear in result.

🔍 Example Using Both WHERE and HAVING


SELECT Department, COUNT(*)
FROM Employees
WHERE Salary > 50000 -- Row filter
GROUP BY Department
HAVING COUNT(*) > 3; -- Group filter

aggregate functions means count, sum, max, min etc like maths

6) What is Sub query or Nested query or Inner query in SQL?


SQL query. The subquery runs first and its result is used by the outer query to further filter or
process data. Subqueries can be used in various parts of a SQL statement such as WHERE,
SELECT, or FROM clauses.

Key points about subqueries:


 They are enclosed in parentheses.
 They return data that the outer query depends on.
 They can return single or multiple rows and columns.
 Types include single-row subqueries, multi-row subqueries, and correlated subqueries that
depend on outer query values.
 Subqueries improve query modularity and enable complex data retrieval by breaking it into
manageable parts.

Example: To find the first name of the oldest customer:

SELECT first_name
FROM Customers
WHERE age = (SELECT MAX(age) FROM Customers);

Here, the inner query selects the maximum age, then the outer query selects the first name
of customers with that age.
Subqueries help structure complex queries and can be combined with aggregate functions,
joins, and other SQL features to enhance data retrieval

7) What is Auto Increment/ Identity column in SQL Server ? (basic question)


An Auto Increment or Identity column in SQL Server is a special type of column that
automatically generates unique numeric values for each new row inserted into a table. This is
typically used for primary key columns to ensure each record has a unique identifier without
manually specifying the value.
In SQL Server, the auto-increment feature is implemented using the IDENTITY property in the
column definition.
The syntax looks like this:

CREATE TABLE Persons (


PersonID int IDENTITY(seed, increment) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255)
);

 The seed is the starting value for the identity column (e.g., 1).
 The increment is the value by which to increase the column for each new record (e.g., 1).
For example, IDENTITY(1,1) starts numbering at 1 and increments by 1 for each new row. You
can customize it like IDENTITY(10,5) to start at 10 and increment by 5.

8) What are Joins in SQL? (important question)


A JOIN in SQL is used to combine rows from two or more tables based on a related column
between them.
Joins help retrieve meaningful information spread across multiple tables in a database.
Example:
 Employees Table
 Departments Table
We join them using DepartmentId.

9) What are the types of Joins in SQL Server? (very important question Ask in tcs,
wipro, Infosys, Accenture etc)

The main types of SQL joins include:

 Inner Join: Returns only the rows where there is a match in both joined tables based
on the join condition.

Example:-

SELECT [Link], [Link]

FROM Employees e

INNER JOIN Departments d

ON [Link] = [Link];

 Left Outer Join (Left Join): Returns all rows from the left table and matched rows
from the right table; non-matching rows from the right table appear as NULL.

example

SELECT [Link], [Link]


FROM Employees e

LEFT JOIN Departments d

ON [Link] = [Link];

 Right Outer Join (Right Join): Returns all rows from the right table and matched rows
from the left table; non-matching rows from the left table appear as NULL.

example

SELECT [Link], [Link]

FROM Employees e

RIGHT JOIN Departments d

ON [Link] = [Link];

 Full Outer Join (Full Join): Returns all rows when there is a match in either left or
right table; unmatched rows in either table appear with NULL for the other side.

Example:-

SELECT [Link], [Link]

FROM Employees e

FULL OUTER JOIN Departments d

ON [Link] = [Link];

 Cross Join: Produces the Cartesian product of the two tables, combining every row of
the first table with every row of the second.

Example:-

SELECT *

FROM Employees

CROSS JOIN Departments;

 Self Join: A join where a table is joined to itself to query hierarchical or relational
data within the same table.

Example:-

SELECT [Link] AS Employee, [Link] AS Manager


FROM Employees e

LEFT JOIN Employees m

ON [Link] = [Link];

10) What is Self-Join ? ( important question)

Self Join: A join where a table is joined to itself to query hierarchical or relational
data within the same table.

Example:-

SELECT [Link] AS Employee,

[Link] AS Manager

FROM Employees e

LEFT JOIN Employees m

ON [Link] = [Link];

11) Write a SQL query to fetch all the Employees who are also Managers? (very
important question Ask in tcs, wipro, Infosys, Accenture etc)

✅ Approach 1: Using Self-Join (Most Common)

To fetch all employees who are also managers, you can use a self join on the
Employees table. The idea is to join the table to itself where the ManagerID of an
employee matches the EmployeeID of another employee (who is thus a manager).
This identifies employees who are referenced as managers by others.

A common query pattern is:

sql

SELECT DISTINCT e.*

FROM Employees e

JOIN Employees m ON [Link] = [Link];

Explanation:

 Employees e is the alias for employees.


 Employees m is another alias for the same table representing managers.
 The join condition [Link] = [Link] finds employees whose ID appears
as a manager ID for others.
 DISTINCT ensures each manager (employee who is also a manager) appears once.

This query returns the rows of employees who have one or more subordinates,
effectively those who are managers as well.

✅ Approach 2: Using IN Clause (Simplest)

SELECT *

FROM Employees

WHERE EmpId IN (SELECT ManagerId FROM Employees WHERE ManagerId IS


NOT NULL);

✔ How it works:

 Subquery gets all manager IDs


 Main query returns employee details of those managers

12) What are Indexes in SQL Server?

An index is a database object created on one or more columns of a table to make data
searching faster.
Instead of scanning the whole table (called a table scan), SQL Server uses the index
to find rows quickly (called an index seek).

13) What is Clustered index ?

“A clustered index sorts and stores the actual data rows of the table in order of the index key.
Its leaf nodes contain the real data. Since the table data can be ordered only one way, a table
can have only one clustered index.”

🧱 Example

CREATE CLUSTERED INDEX IX_Employee_EmployeeId

ON Employees(EmployeeId);

Now the table rows are stored in the order of EmployeeId.

14) What is Non-Clustered index ?


A Non-Clustered Index is an index in SQL Server that creates a separate structure
from the actual table data.
It stores only the indexed column values and a pointer (row locator) to the actual
data row.

It does not change the physical order of data in the table.

A table can have multiple non-clustered indexes.

✅ Basic Non-Clustered Index (Most Common Interview Answer)

CREATE NONCLUSTERED INDEX IX_Employees_Name

ON Employees(Name);

✔ Creates a non-clustered index on the Name column.


✔ Best for queries that search by Name.

15) What is the difference between Clustered and Non-Clustered index?(very important
question)

“A clustered index stores the actual data in sorted order and there can be only
one per table. Its leaf nodes contain the real data.
A non-clustered index is a separate structure that stores only the index key and a
pointer to the data, allowing multiple non-clustered indexes per table.”

16) How to create Clustered and Non-Clustered index in a table?

CLUSTERED INDEX

When you create a PRIMARY KEY constraint, a clustered index on the column is
automatically created.

syntax

CREATE CLUSTERED INDEX index_name

ON table_name (column_name);

query

CREATE CLUSTERED INDEX IX_Employees_EmployeeId

ON Employees(EmployeeId);

NON- CLUSTERED INDEX


syntax

CREATE NONCLUSTERED INDEX index_name

ON table_name (column1, column2);

Query

CREATE NONCLUSTERED INDEX IX_Employees_Name_City

ON Employees(Name, City);

17) in which Column you will apply the indexing to optimize this query. "select id, class
from student where name=”happy””? (very important question)

To optimize the query:

sql

select id, class from student where name = 'happy';

You should apply the index on the column used in the WHERE clause, which is the
"name" column.

Reasoning:

 Indexing the "name" column allows the database to quickly locate rows where the
name is "happy" without scanning the entire table.
 The "id" and "class" columns are just selected but not involved in filtering, so
indexing them does not contribute to query filtering performance.
 Proper indexing on the column used in the WHERE clause is a best practice for query
optimization.

Thus, creating an index on the "name" column will make this query more efficient.

Example: Creating a Non-Clustered Index on name

CREATE NONCLUSTERED INDEX IX_Student_Name

ON student(name);

18) What is the difference between Stored Procedure and Functions? (very important
question Ask in tcs, wipro, Infosys, Accenture etc)

 Stored Procedures can have both input and output parameters, but Functions
only accept input parameters.
 We cannot use Stored Procedures (sp) in SQL statements like SELECT,
UPDATE, INSERT, DELETE, MERGE etc, but we can use them with
Function.
 We can call Functions inside Stored Procedures(SP), but cannot call Stored
Procedures from a Functions.
 A Stored Procedure supports TRY–CATCH and transactions, but a Function
does not support TRY–CATCH or transactions.
 SP may or may not return a value but Function must return a value.
 A Stored Procedure is called using EXEC, but a Function is called inside a
SELECT statement.
 Stored Procedures can use temporary tables and dynamic SQL, but Functions
cannot use temporary tables and have limited SQL capabilities.

19) How to optimize a Stored Procedure or SQL Query? (very important question Ask in
tcs, wipro, Infosys, Accenture etc)

 Optimize Your Queries: Write efficient SQL by avoiding SELECT *,


specifying only required columns, and using appropriate WHERE clauses to
filter data early

Example: SELECT id, name instead of SELECT *

 Use Schema Names: Always specify schema names when referencing tables
or procedures to reduce object lookup time.

Example: SELECT EmpID, Name from [Link]

dbo is a Schema

 Use SET NOCOUNT ON: This prevents unnecessary message generation


about affected rows, improving performance in stored procedures.
 Use EXISTS() Instead of COUNT().

Example: SELECT Count(1) From [Link]

Example: IF( EXISTS (SELECT 1 From [Link]))

 Use Transaction when required only

20) What is a Cursor? Why to avoid them?

A cursor is a row-by-row data processing mechanism in SQL. We avoid them because


they are slow, consume high memory, cause blocking, and don’t scale well compared
to set-based SQL operations.
21) What is CTE in SQL Server ? (very important question Ask in tcs, wipro, Infosys,
Accenture etc)

A CTE (Common Table Expression) is a temporary, named result set that you can
reference within a SELECT, INSERT, UPDATE, or DELETE statement.

🎯 One-Line Interview Answer

A CTE is a temporary named result set defined using the WITH clause, used to
simplify complex queries, improve readability, and support recursive queries
(like hierarchical data).

22) What is the difference between Delete, Truncate and Drop commands? (very
important question Ask in tcs, wipro, Infosys, Accenture etc)

Feature DELETE TRUNCATE DROP

DML (Data DDL (Data


Command Manipulation Definition DDL (Data Definition
Type Language) Language) Language)

Deletes specific
rows based on a Deletes all rows
condition (can from a table, but Deletes the entire
delete all rows with retains the table table and its structure
Purpose no condition) structure from the database

Data Removes selected Removes the table and


Removal rows Removes all rows all its data

Use of
WHERE
Clause Supported Not supported Not applicable

Does not fire


Triggers Fires triggers triggers Does not fire triggers

Rollback Cannot be rolled


Support Can be rolled back back Cannot be rolled back

Faster than
Speed and Slower, processes DELETE, locks Fastest, removes table
Efficiency row-by-row entire table instantly

Impact on Structure retained,


Table No impact; structure identity counters Table and structure
Structure is retained reset deleted
Feature DELETE TRUNCATE DROP

DELETE FROM
Employees
WHERE TRUNCATE DROP TABLE
Department = 'HR'; TABLE Employees; Employees;

23) How to get the Nth highest salary of an employee? (very important question Ask in
tcs, wipro, Infosys, Accenture etc)

To get the Nth highest salary of an employee in SQL Server, a common and efficient
approach is to use the ROW_NUMBER() window function or the TOP keyword with
nested queries.

Here are some typical methods:

1. Using ROW_NUMBER():

sql

SELECT emp_name, salary

FROM (

SELECT emp_name, salary,

ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num

FROM Employee

) AS ranked_salaries

WHERE row_num = N;

This assigns row numbers ordered by salary descending and selects the row with the
number N.

2. Using TOP with nested queries:

sql

SELECT TOP 1 salary

FROM (

SELECT DISTINCT TOP N salary

FROM Employee
ORDER BY salary DESC

) AS temp

ORDER BY salary ASC;

This selects the top N distinct salaries in descending order, then picks the smallest one
among them, which corresponds to the Nth highest salary.

3. Using a correlated subquery:

sql

SELECT MAX(salary)

FROM Employee

WHERE salary < (

SELECT MAX(salary)

FROM Employee

WHERE salary < (

-- Repeat N-1 times to go down N ranks

);

Though less efficient, it finds the Nth maximum salary by repeatedly filtering max
salaries.

The first method using ROW_NUMBER() is often preferred in SQL Server due to its
clarity and efficiency.

24) what is legal expression in sql?

A legal expression in SQL is an expression that is valid, syntactically correct, and


follows SQL rules so that the database engine can evaluate it to produce a result.

✅ Examples of Legal Expressions

1. Arithmetic Expressions

SELECT 10 + 5;
SELECT Salary * 1.10;

2. Comparison Expressions

Salary > 50000

Age <= 30

3. Logical Expressions

Salary > 50000 AND Department = 'IT'

4. Column Expressions

SELECT (Salary + Bonus) AS TotalSalary

FROM Employees;

5. Function Expressions

SELECT UPPER(Name)

FROM Employees;

❌ Examples of Illegal (Invalid) Expressions

Expressions that break SQL rules:

❌ Incorrect syntax

SELECT Salary + FROM Employees; -- Missing value

❌ Wrong operators

SELECT Salary === 50000; -- Invalid operator

❌ Mismatched data types

SELECT Salary + 'ABC'; -- Can't add string to number


(in strict SQL)

25) find duplicate values? Or find how many times duplicate values are repeated?

Syntax:-

SELECT column_name, COUNT(*) AS repeat_count


FROM table_name

GROUP BY column_name

HAVING COUNT(*) > 1;

Find Duplicate Values in a Single Column

Example: Find duplicate names in a student table

SELECT name, COUNT(*) AS duplicate_count

FROM student

GROUP BY name

HAVING COUNT(*) > 1;

Find Duplicate Records Based on Multiple Columns

Example: Find duplicates by name + class

SELECT name, class, COUNT(*) AS duplicate_count

FROM student

GROUP BY name, class

HAVING COUNT(*) > 1;

Find Full Duplicate Rows (All Columns Match)

SELECT *, COUNT(*) AS duplicate_count

FROM student

GROUP BY id, name, class

HAVING COUNT(*) > 1;

26) we can Delete Duplicate values from columns?


Yes, you can delete duplicate values from columns, but SQL never deletes values
inside a column directly — instead, it deletes entire duplicate rows based on the
column(s) you choose.

Important
SQL cannot delete only the value inside a cell.
It can only delete the whole row that contains the duplicate value.

27) How to delete duplicate rows ?

Use a Common Table Expression (CTE) with the ROW_NUMBER() window


function to number duplicates within groups and then delete all rows where this
number is greater than 1:

WITH CTE AS (

SELECT *,

ROW_NUMBER() OVER (PARTITION BY [columns that define a duplicate]


ORDER BY [some column]) AS RowNum

FROM TableName

DELETE FROM CTE WHERE RowNum > 1;

This approach keeps the first row found for each duplicate set and deletes the rest.

 Alternatively, you can delete using a subquery that keeps the row with the
minimum id (or any unique identifier):

sql

DELETE FROM tableName

WHERE id NOT IN (

SELECT MIN(id)

FROM tableName

GROUP BY column1, column2, ...

);

This removes duplicates while preserving one record from each duplicate group
28) list the negative integer values from column in sql?

To list the negative integer values from a column in SQL, use a WHERE clause that
checks for both negativity and integer status. If the column only contains integer data
(like INT type), the query is straightforward:

SELECT column_name

FROM table_name

WHERE column_name < 0;

🔍 Example

Table: Transactions
Column: Amount

SELECT Amount

FROM Transactions

WHERE Amount < 0;

This will return all amounts that are negative

29) difference between data table and dataset?

30)

You might also like