0% found this document useful (0 votes)
5 views

SQL Interview Questions 1725044566

Uploaded by

hari.3kblr1387
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL Interview Questions 1725044566

Uploaded by

hari.3kblr1387
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Difference Between in SQL Interview Questions

Primary Key Foreign Key


References the Primary Key in the parent
Avoids duplicates and nulls.
table.
Acts as a combination of UNIQUE and NOT
Allows duplicates.
NULL constraints.
Always attached to the parent table. Always attached to the child table.
Creates a unique index by default. Does not create an index by default.
Can be added at the table creation, alter, or
Can be added at the table or alter level only.
column level.

DELETE TRUNCATE
Removes specific rows based on a condition. Removes all rows from the table.
Can be rolled back if used within a
Cannot be rolled back once executed.
transaction.
Slower as it logs individual row deletions. Faster as it does not log individual deletions.
Activates triggers if present. Does not activate triggers.
Leaves table structure and indexes intact. Resets table structure and reclaims space.

WHERE HAVING
Filters rows before grouping or aggregation. Filters groups after aggregation.
Used with SELECT, UPDATE, and DELETE
Used only with SELECT statements.
statements.
Cannot use aggregate functions (e.g., SUM,
Can use aggregate functions.
COUNT).
Filters groups of rows based on aggregate
Filters individual rows based on conditions.
conditions.
Applied before any grouping is done in a
Applied after the GROUP BY clause in a query.
query.

RANK DENSE_RANK
Assigns a unique rank to each row within a
Similar to RANK but without gaps in ranking.
partition of a result set.
Skips ranks when there are ties (e.g., 1, 2, 2, Does not skip ranks; consecutive ranks (e.g., 1,
4). 2, 2, 3).
Introduces gaps if there are ties in the
No gaps; ranks increase consecutively.
ranking.
Syntax: RANK() OVER (PARTITION BY ... Syntax: DENSE_RANK() OVER (PARTITION
ORDER BY ...). BY ... ORDER BY ...).
Ideal when gaps in rank numbers are Ideal when continuous ranking without gaps is
acceptable. needed.

Follow me on LinkedIn – Shivakiran kotur


LEAD LAG
Retrieves data from the next row in the same Retrieves data from the previous row in the
result set. same result set.
Helps compare the current row with future Helps compare the current row with previous
rows. rows.
Syntax: LEAD(column, offset, default) OVER Syntax: LAG(column, offset, default) OVER
(PARTITION BY ... ORDER BY ...). (PARTITION BY ... ORDER BY ...).
Useful for calculating future trends or Useful for calculating past trends or
forecasts. comparisons.
Offset is by default 1 if not specified. Offset is by default 1 if not specified.

Primary Key Surrogate Key


An artificial, system-generated identifier,
A natural identifier for a record, often derived
usually numeric (e.g., an auto-increment
from real-world data.
column).
Must be unique and not null, often a
Must be unique and not null.
sequential number.
Values have business meaning and can be Values have no business meaning, used only
used in queries by users. for database management.
Can be a single column or a combination of
Typically a single column, often an integer.
columns.
Can be affected by changes in the data source Not affected by changes in the data source;
(e.g., name changes). remains stable.

VIEW MATERIALIZED VIEW


Has a logical existence; does not store data. Has a physical existence; stores data.
Cannot perform DML operations on complex
DML operations can be performed.
views.
Fetches data from the base table when queried. Fetches data from the stored, materialized view.
Cannot be scheduled to refresh automatically. Can be scheduled to refresh automatically.
Can store aggregated data, often used for
Does not store aggregated data.
reporting purposes.

Sub-Query Correlated Sub-Query


Executed once for the entire parent query. Executed once for each row of the parent query.
Independent of the parent query. Depends on the parent query for its values.
Example: Example:
SELECT * FROM Emp SELECT e.* FROM Emp e
WHERE Deptno IN WHERE Sal >=
(SELECT Deptno FROM Dept); (SELECT AVG(Sal) FROM Emp a
WHERE a.Deptno = e.Deptno
G GROUP BY a.Deptno);

Follow me on LinkedIn – Shivakiran kotur


Stored Procedure Function
May or may not return values. Must return at least one output value.
Can return multiple values using OUT
Typically returns a single value.
parameters.
Used to implement complex business logic and Primarily used for calculations and data
operations. manipulation.
Not pre-compiled; parsed and compiled at
Pre-compiled and optimized for performance.
runtime.
Can accept multiple arguments but returns a
Can accept multiple arguments.
single result.
Mainly used to process tasks and perform
Mainly used to compute values.
operations.
Cannot be directly invoked from SQL statements Can be invoked from SQL statements (e.g.,
(e.g., SELECT). SELECT).

Triggers Stored Procedures


No need to execute manually; fired Need to be executed manually or called
automatically based on events. explicitly.
Execute automatically when an INSERT, UPDATE, Typically used for performing tasks and
or DELETE is issued. operations.
Primarily used for tracing, auditing, and Used to encapsulate logic and perform
enforcing business rules. operations on data.
Can run independently and be executed from
Part of DML events on the table.
various contexts.
Can have parameters and be executed from
Cannot be executed from stored procedures.
other stored procedures.
Cannot have parameters. Can have parameters.
Event-driven and attached to a specific table or A compiled collection of SQL statements or
database object. programs.

OLTP OLAP
Designed for managing transactional data. Designed for analytical and reporting purposes.
Handles a large number of short online Handles complex queries involving data
transactions (e.g., INSERT, UPDATE, DELETE). aggregation and analysis.
Data is often denormalized to optimize query
Data is highly normalized to reduce redundancy.
performance.
Focuses on speed and efficiency for daily Focuses on query performance and data
operations. retrieval speed for analysis.
Typical operations include order entry, Typical operations include data mining,
payments, and customer management. forecasting, and business reporting.
Databases are typically large, integrating data
Databases are typically smaller in size.
from multiple sources.
Examples: Banking systems, e-commerce Examples: Data warehouses, business
websites. intelligence systems.

Follow me on LinkedIn – Shivakiran kotur


Data Mart Data Warehouse
A "Subject-Oriented, Integrated, Time-Variant,
Usually sponsored at the department level with
Non-Volatile" collection of data supporting
a specific focus or subject.
decision-making.
Used at a business division or department level. Used at an enterprise level.
An integrated consolidation of data from various
A subset of data from a Data Warehouse, built
sources for strategic and tactical decision-
for specific user groups.
making.
Provides a comprehensive and coherent view of
Provides a focused subset of data, which can
the business, integrating data across the
improve privacy, performance, and clarity.
enterprise.
Developed to support overall business
Developed to address specific issues or needs
intelligence and decision-making across the
within a department.
organization.

Star Schema Snowflake Schema


Simplest data warehouse schema. More complex data warehouse model.
Each dimension is represented in a single table; At least one hierarchy exists between dimension
no hierarchies between dimensions. tables.
Contains a central fact table surrounded by Contains a central fact table surrounded by
dimension tables. normalized dimension tables.
Only one join is needed between the fact table Requires multiple joins due to relationships
and dimension tables. between dimension tables.
Normalizes dimensions to eliminate
Optimizes performance with simple queries and
redundancy, leading to more complex queries
fast response times.
and potentially reduced performance.

Follow me on LinkedIn – Shivakiran kotur

You might also like