SQL Interview Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

SQL Interview questions

„Give me more relational database management and data modeling interview questions and
replies.”

1. What is a relational database management system (RDBMS)?


Response: A relational database management system (RDBMS) is a software
system that is used to manage relational databases. It provides tools and utilities
for storing, retrieving, updating, and managing data in a structured format based
on the relational model. Examples of RDBMS include MySQL, PostgreSQL, Oracle
Database, SQL Server, and SQLite.
2. What is normalization and why is it important in database design?
Response: Normalization is the process of organizing data in a database to
minimize redundancy and dependency. It involves breaking down large tables
into smaller, more manageable tables and defining relationships between them.
Normalization helps ensure data integrity, reduces data redundancy, and
improves database performance by eliminating anomalies such as insertion,
update, and deletion anomalies.
3. Explain the difference between primary key and foreign key.
Response:
• Primary key: A primary key is a column or combination of columns that
uniquely identifies each row in a table. It ensures data integrity and serves
as the primary means of accessing and referencing data in the table.
• Foreign key: A foreign key is a column or combination of columns that
establishes a relationship between two tables. It refers to the primary key
of another table and enforces referential integrity by ensuring that the
values in the foreign key column(s) match the values in the primary key
column(s) of the referenced table.
4. What are the different types of relationships in database design?
Response: The main types of relationships in database design are:
• One-to-One (1:1): Each record in one table is related to only one record
in another table.
• One-to-Many (1:N): Each record in one table can be related to multiple
records in another table, but each record in the second table is related to
only one record in the first table.
• Many-to-Many (M:N): Multiple records in one table can be related to
multiple records in another table through a linking table (also known as
junction or associative table).
5. What is denormalization and when is it used?
Response: Denormalization is the process of intentionally introducing
redundancy into a database design for the purpose of improving performance or
simplifying queries. It involves combining tables or duplicating data to reduce the
number of joins required to retrieve data. Denormalization is often used in data
warehousing, reporting, and analytical systems where read performance is critical
and data integrity constraints can be relaxed.
6. Explain the concept of cardinality in data modeling.
Response: Cardinality refers to the relationship between two entities in a data
model and describes the number of instances of one entity that are associated
with a single instance of another entity. Cardinality is typically expressed as one
of the following:
• One-to-One (1:1): Each instance of one entity is associated with exactly
one instance of another entity.
• One-to-Many (1:N): Each instance of one entity is associated with zero or
more instances of another entity.
• Many-to-Many (M:N): Each instance of one entity is associated with zero
or more instances of another entity, and vice versa.
7. What is an ER diagram and how is it used in database design?
Response: An Entity-Relationship (ER) diagram is a visual representation of the
entities, attributes, and relationships in a database. It uses various symbols such
as rectangles (entities), diamonds (relationships), and ovals (attributes) to depict
the structure of the database. ER diagrams help database designers understand
the relationships between different entities and design databases that accurately
represent the real-world domain.

8. What are the ACID properties in database transactions?


Response: The ACID properties stand for Atomicity, Consistency, Isolation, and
Durability, which are essential characteristics of database transactions:
• Atomicity: Ensures that a transaction is treated as a single unit of work
and either all of its operations are successfully completed or none of them
are.
• Consistency: Ensures that the database remains in a consistent state
before and after the transaction, preserving integrity constraints and data
validity.
• Isolation: Ensures that the execution of one transaction is isolated from
the effects of other concurrent transactions, preventing interference and
maintaining data integrity.
• Durability: Ensures that once a transaction is committed, its changes are
permanently stored in the database and are not lost, even in the event of
system failure.
9. What is a surrogate key and when would you use it?
Response: A surrogate key is a unique identifier assigned to each record in a
table that has no inherent meaning or business significance. It is typically
implemented as an auto-incrementing integer or a globally unique identifier
(GUID). Surrogate keys are commonly used as primary keys in tables where there
is no natural or unique key available, or where using a surrogate key simplifies
database operations and improves performance.
10. Explain the difference between a logical data model and a physical data
model.
Response:
• Logical Data Model: A logical data model describes the structure of the
data without considering the physical implementation details such as
storage mechanisms, indexes, or partitioning. It focuses on the entities,
attributes, relationships, and constraints within the data model, often
represented using entity-relationship diagrams (ERDs) or Unified Modeling
Language (UML) diagrams.
• Physical Data Model: A physical data model represents how the logical
data model will be implemented in the physical database system. It
includes details such as table structures, data types, indexes, partitions,
storage options, and optimization techniques tailored to the specific
database platform being used. The physical data model is optimized for
performance, scalability, and storage efficiency.
11. What is a foreign key constraint and why is it important?
Response: A foreign key constraint is a rule that enforces referential integrity
between two related tables in a relational database. It ensures that the values in a
column (or combination of columns) in one table match the values in a primary
key column (or unique key column) in another table. Foreign key constraints
prevent orphaned records, maintain data consistency, and enforce the
relationships defined in the database schema.
12. Describe the difference between a self-referencing table and a recursive
relationship in data modeling.
Response:
• Self-Referencing Table: A self-referencing table is a table in which a
column (or set of columns) references the primary key of the same table.
This creates a hierarchical or recursive relationship within the table itself,
such as a hierarchical organizational chart where each employee reports to
another employee in the same table.
• Recursive Relationship: A recursive relationship occurs when an entity
has a relationship with itself, either directly or indirectly. This can be
represented in a data model using self-referencing foreign keys or
recursive queries. Recursive relationships are commonly used to model
hierarchical structures, such as organizational hierarchies, bill of materials,
or tree structures.
13. What is the difference between a join and a union in SQL?
Response:
• Join: A join is used to combine rows from two or more tables based on a
related column between them. It retrieves data by matching rows from
different tables based on a common column or relationship, such as
INNER JOIN, LEFT JOIN, RIGHT JOIN, or FULL JOIN.
• Union: A union is used to combine the results of two or more SELECT
statements into a single result set. It retrieves distinct rows from multiple
SELECT statements and concatenates the results vertically, removing
duplicate rows by default.
14. Explain the concept of cardinality and participation constraints in data
modeling.
Response:
• Cardinality: Cardinality describes the relationship between two entities in
terms of the number of instances of one entity that can be associated with
a single instance of another entity. It is typically expressed as one-to-one
(1:1), one-to-many (1:N), or many-to-many (M:N) relationships.
• Participation Constraints: Participation constraints specify whether each
entity in a relationship must participate in the relationship. They can be
total (mandatory) or partial (optional) for each entity involved. Total
participation means every entity in the relationship must participate, while
partial participation means some entities may not participate.
15. What are the advantages and disadvantages of denormalization in database
design?
Response:
• Advantages: Denormalization can improve query performance by
reducing the number of joins needed to retrieve data, simplify complex
queries, and eliminate the need for certain types of aggregations. It can
also improve data retrieval speed and reduce disk I/O.
• Disadvantages: Denormalization can lead to data redundancy and
increase storage requirements, potentially resulting in data inconsistency if
updates are not properly managed. It can also complicate data
maintenance and increase the risk of update anomalies.
16. What is normalization and why is it important in database design?
Response: Normalization is the process of organizing data in a database to
minimize redundancy and dependency by dividing large tables into smaller tables
and defining relationships between them. It helps ensure data integrity, reduces
storage requirements, and improves query performance by eliminating duplicate
data and reducing the risk of update anomalies.
17. Explain the First Normal Form (1NF) and provide an example.
Response: First Normal Form (1NF) requires that each column in a table must
contain atomic values, and each table cell should hold only a single value. It
eliminates repeating groups and ensures that each attribute represents a single
piece of information. For example, consider a table Employee with columns
EmployeeID, Name, and PhoneNumbers. To normalize it to 1NF, we would create a
separate table EmployeePhoneNumbers with columns EmployeeID and PhoneNumber,
where each employee can have multiple phone numbers.
18. What is denormalization and when would you use it?
Response: Denormalization is the process of intentionally adding redundancy to
a database schema to improve query performance by reducing the number of
joins needed to retrieve data. It involves duplicating data or incorporating
derived data into tables to eliminate the need for complex joins or aggregations.
Denormalization is typically used in read-heavy systems or when performance
optimization is a primary concern, such as data warehousing or reporting
applications.
19. Describe the different types of database indexes and their use cases.
Response:
• Primary Index: A primary index is created on the primary key of a table to
enforce uniqueness and provide fast access to rows. It is automatically
created when defining a primary key constraint.
• Secondary Index: A secondary index is created on non-primary key
columns to speed up data retrieval for frequently queried columns. It can
be created on single or multiple columns.
• Clustered Index: A clustered index determines the physical order of data
rows in a table, usually based on the primary key. It can improve data
retrieval performance for range queries but can slow down insert and
update operations.
• Non-Clustered Index: A non-clustered index creates a separate data
structure that points to the rows in the table, allowing for fast retrieval of
data based on the indexed columns without affecting the physical order of
the data.
20. Explain the concept of cardinality and participation constraints in ER
modeling.
Response:
• Cardinality: Cardinality describes the relationship between two entities in
terms of the number of instances of one entity that can be associated with
a single instance of another entity. It is typically expressed as one-to-one
(1:1), one-to-many (1:N), or many-to-many (M:N) relationships.
• Participation Constraints: Participation constraints specify whether each
entity in a relationship must participate in the relationship. They can be
total (mandatory) or partial (optional) for each entity involved. Total
participation means every entity in the relationship must participate, while
partial participation means some entities may not participate.
21. What is a surrogate key, and when would you use it?
Response: A surrogate key is an artificial primary key assigned to each record in
a table for the sole purpose of uniquely identifying each row. It is typically an
auto-incrementing integer or a globally unique identifier (GUID). Surrogate keys
are used when there is no suitable natural key available, or when using a
surrogate key simplifies database operations and improves performance.
22. Explain the difference between a left join and an inner join.
Response:
• Inner Join: An inner join returns only the rows from both tables that
satisfy the join condition. It combines rows from two tables based on a
related column or relationship, discarding unmatched rows from either
table.
• Left Join (or Left Outer Join): A left join returns all the rows from the left
table and matching rows from the right table based on the join condition.
If there is no matching row in the right table, NULL values are included for
the columns from the right table in the result set.
23. What is a data warehouse, and how does it differ from a traditional
database?
Response: A data warehouse is a centralized repository for storing and
managing large volumes of structured and unstructured data from multiple
sources. It is designed for analytical and reporting purposes, providing a
consolidated view of data for decision-making. Unlike traditional transactional
databases, data warehouses are optimized for read-heavy workloads, historical
data analysis, and complex queries. They often use denormalized schemas,
dimensional modeling techniques, and OLAP (Online Analytical Processing) tools
for data analysis and reporting.
24. Explain the difference between OLTP and OLAP databases.
Response:
• OLTP (Online Transaction Processing): OLTP databases are designed for
transactional processing and support day-to-day operations of an
organization. They are optimized for fast data inserts, updates, and
deletions, and are characterized by normalized schemas to minimize data
redundancy. Examples include CRM systems, e-commerce platforms, and
banking applications.
• OLAP (Online Analytical Processing): OLAP databases are designed for
analytical processing and support complex queries and data analysis. They
are optimized for read-heavy workloads and provide aggregated and
summarized views of data for reporting and decision-making. OLAP
databases typically use denormalized schemas, dimensional modeling
techniques, and multidimensional data structures. Examples include data
warehouses, business intelligence systems, and reporting tools.
25. What is a foreign key, and how does it enforce referential integrity?
Response: A foreign key is a column or combination of columns in a table that
establishes a link or relationship between two tables. It represents a field in one
table that refers to the primary key in another table. Foreign keys enforce
referential integrity by ensuring that every value in the foreign key column of a
child table corresponds to a value in the primary key column of the parent table.
This prevents orphaned or invalid references and maintains data consistency
between related tables.
26. Explain the concept of data redundancy and how it can be minimized in
database design.
Response: Data redundancy refers to the duplication of data within a database,
which can lead to inconsistency, wasted storage space, and increased update
anomalies. It can be minimized through normalization techniques, such as
breaking large tables into smaller tables and defining relationships between
them, to eliminate repeating groups and redundant attributes. Additionally,
denormalization can be used judiciously to introduce controlled redundancy for
performance optimization while ensuring data integrity.
27. What is a composite key, and when would you use it?
Response: A composite key is a key that consists of two or more columns in a
table that together uniquely identify each row. It is used when a single column
cannot uniquely identify rows in a table, but a combination of multiple columns
can. Composite keys are commonly used in junction tables for many-to-many
relationships and in bridge tables for resolving many-to-many relationships in a
relational database schema.
28. Describe the ACID properties of database transactions.
Response: ACID stands for Atomicity, Consistency, Isolation, and Durability,
which are four key properties that ensure the reliability and integrity of database
transactions:
• Atomicity: Atomicity ensures that all operations within a transaction are
completed successfully or none of them are. If any part of a transaction
fails, the entire transaction is rolled back, and the database is left
unchanged.
• Consistency: Consistency ensures that a transaction brings the database
from one valid state to another valid state, preserving data integrity and
enforcing constraints.
• Isolation: Isolation ensures that the execution of one transaction is
isolated from other transactions, preventing interference and maintaining
data integrity in a multi-user environment.
• Durability: Durability ensures that the effects of a committed transaction
persist even in the event of system failures or crashes. Once a transaction
is committed, its changes are permanently stored in the database and
cannot be undone.
29. Explain the difference between a view and a materialized view.
Response:
• View: A view is a virtual table that is defined by a query and does not
store data physically. It presents the results of a query as a table, allowing
users to query and manipulate data as if it were stored in a real table.
Views are dynamically generated and updated whenever the underlying
data changes.
• Materialized View: A materialized view is a physical copy of a view that
stores the results of a query as a table. Unlike views, materialized views are
precomputed and stored persistently in the database, which improves
query performance by avoiding the need to recompute the results every
time the view is queried. However, materialized views need to be refreshed
periodically to synchronize them with the underlying data.
30. What are the advantages and disadvantages of denormalization in database
design?
Response:
• Advantages: Denormalization can improve query performance by
reducing the number of joins and aggregations needed to retrieve data,
especially in read-heavy systems. It can also simplify data access and
improve scalability by eliminating the need for complex join operations.
• Disadvantages: Denormalization can increase data redundancy and
storage requirements, leading to potential update anomalies and
inconsistency. It can also make data maintenance more complex and time-
consuming, as changes to denormalized data may need to be propagated
across multiple tables. Additionally, denormalization can degrade write
performance and increase the risk of data anomalies if not carefully
managed.
31. What is database normalization, and why is it important?
Response: Database normalization is the process of organizing data in a
database to minimize redundancy and dependency by dividing large tables into
smaller tables and defining relationships between them. It ensures data integrity,
reduces data duplication, and improves database performance by eliminating
update anomalies and inconsistencies. Normalization also simplifies database
design, maintenance, and querying.
32. Explain the different normal forms in database normalization.
Response:
• First Normal Form (1NF): Ensures that each column contains atomic
values and that there are no repeating groups within rows.
• Second Normal Form (2NF): Ensures that each non-key attribute is fully
functionally dependent on the entire primary key, eliminating partial
dependencies.
• Third Normal Form (3NF): Ensures that each non-key attribute is fully
functionally dependent on the primary key and eliminates transitive
dependencies between non-key attributes.
• Boyce-Codd Normal Form (BCNF): A stricter form of 3NF that applies
when there are multiple candidate keys.
• Fourth Normal Form (4NF): Ensures that there are no multi-valued
dependencies between attributes.
• Fifth Normal Form (5NF): Ensures that every join dependency is implied
by the candidate keys.
33. What is a surrogate key, and when would you use it?
Response: A surrogate key is an artificial key that is generated by the database
system to uniquely identify each row in a table. It is typically used when there is
no natural or meaningful candidate key available or when the available candidate
keys are unsuitable for use as primary keys (e.g., composite keys, non-integer
values). Surrogate keys are often implemented as auto-incrementing integers or
globally unique identifiers (GUIDs) and provide a stable, efficient, and reliable
means of identification.
34. Explain the concept of cardinality in data modeling.
Response: Cardinality refers to the relationship between two entities in a data
model and describes the number of instances of one entity that are associated
with a single instance of another entity. It is expressed using cardinality ratios,
such as one-to-one (1:1), one-to-many (1:N), and many-to-many (N:M).
Cardinality helps define the nature and constraints of relationships between
entities and influences database design decisions, such as table structure, foreign
key constraints, and query optimization.
35. What is the difference between a primary key and a unique key?
Response:
• Primary Key: A primary key is a special type of unique key that uniquely
identifies each row in a table and ensures data integrity by enforcing entity
integrity and uniqueness constraints. There can be only one primary key in
a table, and it cannot contain null values.
• Unique Key: A unique key is a constraint that ensures the uniqueness of
values in one or more columns of a table, similar to a primary key.
However, unlike a primary key, a unique key can contain null values, and
there can be multiple unique keys in a table.
36. What is a data dictionary, and why is it useful in database management?
Response: A data dictionary is a centralized repository or metadata repository
that stores detailed information about the structure, definitions, and attributes of
data elements, tables, columns, relationships, and constraints in a database. It
serves as a comprehensive reference guide for database administrators,
developers, and users, providing valuable insights into the database schema, data
definitions, and business rules. A data dictionary facilitates data governance,
documentation, data lineage, data quality management, and data security,
ensuring consistency, accuracy, and integrity of data across the organization.

„Give me more SQL interview questions and replies.”

1. What is SQL?
Response: SQL, or Structured Query Language, is a standardized programming
language used to manage and manipulate relational databases. It allows users to
perform various operations such as querying data, inserting or updating records,
creating or modifying database structures, and managing permissions.
2. What are the different types of SQL commands?
Response: SQL commands can be categorized into four main types:
• Data Definition Language (DDL): Used to define, modify, and delete
database objects such as tables and indexes. Examples include CREATE,
ALTER, and DROP.
• Data Manipulation Language (DML): Used to manipulate data within
database objects. Examples include SELECT, INSERT, UPDATE, and DELETE.
• Data Control Language (DCL): Used to control access to database objects.
Examples include GRANT and REVOKE.
• Transaction Control Language (TCL): Used to manage transactions within a
database. Examples include COMMIT, ROLLBACK, and SAVEPOINT.
3. What is a primary key?
Response: A primary key is a column or combination of columns that uniquely
identifies each row in a table. It ensures that each row in the table is unique and
serves as the basis for establishing relationships between tables in a relational
database.
4. What is the difference between INNER JOIN and LEFT JOIN?
Response:
• INNER JOIN: Retrieves records that have matching values in both tables
based on the specified join condition. Rows from both tables that do not
meet the join condition are excluded from the result set.
• LEFT JOIN: Retrieves all records from the left table (the first table specified
in the query) and the matched records from the right table based on the
join condition. If there are no matching records in the right table, NULL
values are returned for the columns from the right table.
5. Explain the difference between WHERE and HAVING clauses in SQL.
Response:
• WHERE clause: Filters rows based on a specified condition before the
data is grouped or aggregated. It is used with SELECT, UPDATE, or DELETE
statements to specify the criteria for selecting or modifying rows.
• HAVING clause: Filters rows based on a specified condition after the data
is grouped or aggregated using GROUP BY. It is used with SELECT
statements to filter the results of aggregate functions such as SUM, AVG,
COUNT, etc.
6. What is a subquery?
Response: A subquery, also known as an inner query or nested query, is a SELECT
statement embedded within another SQL statement. It allows you to retrieve data
from one or more tables based on the results of another query. Subqueries can
be used in SELECT, INSERT, UPDATE, and DELETE statements.
7. Explain the difference between UNION and UNION ALL.
Response:
• UNION: Combines the results of two or more SELECT statements and
removes duplicate rows from the result set.
• UNION ALL: Combines the results of two or more SELECT statements,
including duplicate rows in the result set.
8. What is the difference between DELETE and TRUNCATE in SQL?
Response:
• DELETE: The DELETE statement is used to remove rows from a table based
on specified conditions. It operates on individual rows and generates a
transaction log for each deleted row, allowing for rollback operations.
DELETE can be rolled back, and triggers can be fired for each deleted row.
• TRUNCATE: The TRUNCATE statement is used to remove all rows from a
table, effectively resetting the table to its initial state. It removes the data
without logging individual row deletions, making it faster than DELETE for
large tables. TRUNCATE cannot be rolled back, and triggers are not fired.
9. What are SQL Joins? Explain different types of SQL Joins.
Response: SQL Joins are used to combine rows from two or more tables based
on related columns between them. The different types of SQL Joins are:
• INNER JOIN: Returns rows that have matching values in both tables
based on the specified join condition.
• LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table
and the matched rows from the right table. If there are no matches, NULL
values are returned for columns from the right table.
• RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right
table and the matched rows from the left table. If there are no matches,
NULL values are returned for columns from the left table.
• FULL JOIN (or FULL OUTER JOIN): Returns all rows when there is a
match in either the left or right table. If there is no match, NULL values are
returned for the columns from the other table.
10. What is the difference between WHERE and HAVING clauses in SQL?
Response:
• WHERE clause: The WHERE clause is used to filter rows based on a
specified condition before the data is grouped or aggregated. It is applied
to individual rows before any grouping or aggregation occurs.
• HAVING clause: The HAVING clause is used to filter group rows based on
a specified condition after the data is grouped or aggregated using the
GROUP BY clause. It is applied to group results after the grouping or
aggregation process.
11. What is a subquery in SQL? Provide an example.
Response: A subquery is a query nested within another SQL query, also known as
an inner query or nested query. It can be used in SELECT, INSERT, UPDATE, or
DELETE statements to return data that is used as a condition or value in the outer
query. Here's an example:
SELECT *
FROM employees
WHERE department_id IN (SELECT department_id FROM
departments WHERE location = 'New York');
12. Explain the difference between UNION and UNION ALL in SQL.
Response:
• UNION: The UNION operator is used to combine the result sets of two or
more SELECT statements into a single result set, removing duplicate rows.
It performs a distinct operation on the result set.
• UNION ALL: The UNION ALL operator is similar to UNION but includes all
rows from the result sets of the SELECT statements, including duplicates. It
does not perform a distinct operation.
13. What is a SQL injection, and how can it be prevented?
Response: SQL injection is a security vulnerability that occurs when an attacker
inserts malicious SQL code into input fields or parameters of SQL queries,
allowing them to execute unauthorized SQL commands or manipulate the
database. It can lead to data theft, data corruption, or unauthorized access to
sensitive information. To prevent SQL injection, developers should use
parameterized queries or prepared statements, input validation and sanitization,
least privilege principle, and secure coding practices such as avoiding dynamic
SQL generation.
13. What is a primary key and a foreign key in SQL?
Response:
• Primary Key: A primary key is a column or set of columns that uniquely
identifies each row in a table. It ensures that each row in a table is unique
and cannot contain null values. Primary keys are used to enforce entity
integrity and are often indexed for faster data retrieval.
• Foreign Key: A foreign key is a column or set of columns in a table that
establishes a relationship with a primary key or unique key in another
table. It ensures referential integrity by enforcing a link between the data
in two related tables. Foreign keys typically represent relationships
between tables, such as parent-child relationships.
14. What is the difference between the CHAR and VARCHAR data types in SQL?
Response:
• CHAR: The CHAR data type is fixed-length and stores characters up to a
specified length. It pads the values with spaces to fill the remaining space
if the input value is shorter than the specified length. CHAR is suitable for
storing fixed-length strings, such as postal codes or state abbreviations.
• VARCHAR: The VARCHAR data type is variable-length and stores
characters up to a specified maximum length. It only uses the necessary
amount of storage space to store the actual data without padding with
spaces. VARCHAR is suitable for storing variable-length strings, such as
names or descriptions.
15. What are the different types of SQL constraints?
Response: SQL constraints are rules that enforce data integrity and ensure the
accuracy and consistency of data in a database. The different types of SQL
constraints include:
• PRIMARY KEY: Ensures uniqueness and identifies each row in a table.
• FOREIGN KEY: Establishes relationships between tables and enforces
referential integrity.
• UNIQUE: Ensures that all values in a column are unique.
• NOT NULL: Ensures that a column cannot contain null values.
• CHECK: Validates data based on a specified condition.
• DEFAULT: Specifies a default value for a column when no value is
provided during insertion.
16. What is a SQL stored procedure? Provide an example.
Response: A SQL stored procedure is a precompiled SQL code block stored in
the database and executed on demand. It can accept input parameters, perform
database operations, and return results. Stored procedures improve performance,
code reusability, and security. Here's an example:
CREATE PROCEDURE GetEmployeeById
@EmployeeId INT
AS
BEGIN
SELECT * FROM Employees WHERE EmployeeId = @EmployeeId;
END
17. Explain the difference between the GROUP BY and ORDER BY clauses in
SQL.
Response:
• GROUP BY: The GROUP BY clause is used to group rows that have the
same values into summary rows, typically to perform aggregate functions
like COUNT, SUM, AVG, etc. It operates on the entire result set and returns
a single row for each group.
• ORDER BY: The ORDER BY clause is used to sort the result set based on
one or more columns in ascending or descending order. It does not affect
the grouping of rows but arranges the rows in a specified order.
18. What is a SQL index, and how does it improve performance?
Response:
• A SQL index is a data structure that improves the speed of data retrieval
operations on a database table by providing quick access to the rows
based on the indexed columns.
• Indexes are created on columns that are frequently used in search, join,
and sort operations.
• They reduce the number of rows that need to be scanned to locate specific
data, resulting in faster query execution times.
• However, indexes also require additional storage space and may impact
the performance of data modification operations like INSERT, UPDATE,
and DELETE.

You might also like