Preguntas SQL
Preguntas SQL
1. What is SQL?
- SQL (Structured Query Language) is a standard programming language for managing and manipulating
relational databases.
- The main types are DDL (Data Definition Language), DML (Data Manipulation Language), DCL (Data Control
Language), and TCL (Transaction Control Language).
- A primary key is a unique identifier for a record in a table, ensuring that no two rows have the same key.
- A foreign key is a field (or a collection of fields) in one table that uniquely identifies a row in another table,
establishing a relationship between the two.
- A unique key ensures that all values in a column are different from one another, allowing NULL values.
- A join is used to combine rows from two or more tables based on a related column. Types include INNER JOIN,
LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
7. What is a subquery?
- A subquery is a query nested inside another query, used to perform operations that depend on the result of the
outer query.
8. What is a view?
- A view is a virtual table based on the result of a SELECT query, which can simplify complex queries.
9. What is an index?
- An index is a database object that improves the speed of data retrieval operations on a database table.
- A stored procedure is a prepared SQL code that can be saved and reused, allowing for modular programming.
- A trigger is a set of SQL statements that automatically execute in response to certain events on a particular
table.
- Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity.
- Denormalization is the process of intentionally introducing redundancy into a database for the purpose of
improving query performance.
- A composite key is a combination of two or more columns in a table that uniquely identifies a row.
- A candidate key is a column, or a set of columns, that can qualify as a potential primary key.
- DELETE removes rows one at a time and can be rolled back; TRUNCATE removes all rows in a table without
logging individual row deletions.
- WHERE is used to filter records before aggregation, while HAVING is used to filter records after aggregation.
- UNION combines the results of two queries and removes duplicates; UNION ALL combines the results without
removing duplicates.
- A cross join returns the Cartesian product of two tables, combining every row of the first table with every row
of the second.
- A correlated subquery is a subquery that references columns from the outer query.
- An alias is a temporary name given to a table or column for the duration of a query.
- COUNT(*) counts all rows, including duplicates and NULLs, while COUNT(column_name) counts only non-NULL
values in that column.
26. What are aggregate functions?
- Aggregate functions perform a calculation on a set of values and return a single value (e.g., SUM, AVG, COUNT).
- Scalar functions return a single value based on the input value (e.g., UPPER, LOWER, ROUND).
- A cursor is a database object used to retrieve, manipulate, and navigate through a result set one row at a time.
- A transaction is a sequence of operations performed as a single logical unit of work, which is either fully
completed or fully rolled back.
- ACID stands for Atomicity, Consistency, Isolation, and Durability, ensuring reliable processing of database
transactions.
- ROLLBACK undoes changes made in the current transaction, while COMMIT saves all changes made during the
transaction.
- A sequence is a database object that generates a sequential number, often used for primary keys.
- CHAR is a fixed-length string data type, while VARCHAR is a variable-length string data type.
- A temporary table is a short-lived table that exists temporarily during a session or transaction.
- A CTE is a temporary result set that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement.
- RANK() provides a rank number with gaps for ties, while DENSE_RANK() provides consecutive rank numbers
without gaps.
- The GROUP BY clause is used to group rows that have the same values in specified columns for aggregation.
38. What is the difference between INNER JOIN and OUTER JOIN?
- INNER JOIN returns rows with matching values in both tables, while OUTER JOIN returns all rows from one
table and matched rows from the other.
- A clustered index determines the physical order of data in a table; there can be only one clustered index per
table.
- A non-clustered index does not alter the physical order of the table and can have multiple instances in a table.
- A bitmap index uses bit arrays for indexing, suitable for columns with a limited number of distinct values.
- OLTP (Online Transaction Processing) focuses on transaction-oriented tasks, while OLAP (Online Analytical
Processing) is used for data analysis and reporting.
- A data warehouse is a centralized repository that stores large amounts of historical data for analysis and
reporting.
- A data mart is a subset of a data warehouse, focused on a specific business line or team.
- ETL (Extract, Transform, Load) is the process of extracting data from different sources, transforming it into a
suitable format, and loading it into a data warehouse.
- A star schema is a type of database schema that organizes data into fact and dimension tables, resembling a
star shape.
- A snowflake schema is a more normalized version of the star schema, where dimension tables are further
broken down into sub-dimensions.
- Data integrity refers to the accuracy and consistency of data over its lifecycle.
- Referential integrity ensures that relationships between tables remain consistent, particularly with foreign
keys.
Advanced SQL Questions
- A pivot table summarizes data from a detailed table, allowing users to analyze it in a multidimensional format.
- You can create a pivot table using the PIVOT function, which allows you to transform rows into columns.
- PIVOT rotates data from rows to columns, while UNPIVOT converts columns back into rows.
- Window functions perform calculations across a set of table rows related to the current row, without collapsing
the result set.
- ROW_NUMBER() assigns a unique sequential integer to rows within a partition of a result set.
- NTILE() divides a result set into a specified number of groups and assigns a group
- LAG() accesses data from a previous row in the same result set without the need for a self-join.
- LEAD() accesses data from the next row in the same result set, similar to LAG() but forward-looking.
- A table stores actual data, while a view is a virtual table that provides a representation of the data from one or
more tables.
60. What is the difference between a materialized view and a regular view?
- A materialized view stores the result of a query physically on disk, allowing for faster access, while a regular
view does not store data.
- Normalization organizes data to reduce redundancy and dependency, improving data integrity and efficiency.
- 1NF (First Normal Form) requires atomicity of values. 2NF (Second Normal Form) eliminates partial
dependencies. 3NF (Third Normal Form) removes transitive dependencies. BCNF (Boyce-Codd Normal Form) is an
enhancement of 3NF that resolves anomalies caused by functional dependencies.
63. What is indexing and how does it improve query performance?
- Indexing creates a data structure that improves the speed of data retrieval operations by providing quick access
paths.
- Pros: Faster data retrieval. Cons: Slower data modification (insert/update/delete) and increased storage
requirements.
- Query optimization is the process of making a query run more efficiently by choosing the best execution plan.
- Methods include using indexes, avoiding SELECT *, optimizing joins, reducing subqueries, and analyzing
execution plans.
- EXPLAIN or EXPLAIN PLAN provides information about how a SQL query will be executed, including the access
methods and join types used.
- A recursive query refers to a query that references itself, often used for hierarchical data.
- EXCEPT returns distinct rows from the first query that are not found in the second, while NOT IN checks for
exclusion of specific values from a list.
71. Write a SQL query to find the second highest salary from a table named `Employees`.
72. Write a SQL query to find the names of employees who have joined in the last 30 days.
SELECT Name FROM Employees WHERE JoinDate >= DATEADD(DAY, -30, GETDATE());
73. Write a SQL query to retrieve the top N records from a table.
SELECT Name, COUNT(*) FROM Employees GROUP BY Name HAVING COUNT(*) > 1;
75. Write a SQL query to delete duplicate records from a table.
WITH CTE AS
76. Write a SQL query to find the nth highest salary from a table.
SELECT DISTINCT Salary FROM Employees ORDER BY Salary DESC OFFSET N-1 ROWS
77. Write a SQL query to find the common records between two tables.
78. Write a SQL query to find the records that are present in one table but not in another.
85. Write a SQL query to remove leading and trailing spaces from a string.
86. Write a SQL query to find the difference between two dates.
92. Write a SQL query to find the number of employees in each department.
93. Write a SQL query to find the total salary of each department.
94. Write a SQL query to find the average salary of each department.
95. Write a SQL query to find the maximum salary of each department.
96. Write a SQL query to find the minimum salary of each department.
97. Write a SQL query to find the department with the highest number of employees.
98. Write a SQL query to find the department with the highest total salary.
99. Write a SQL query to find the employees who earn more than the average salary.
SELECT * FROM Employees WHERE Salary > (SELECT AVG(Salary) FROM Employees);
100. Write a SQL query to find the employees who earn the highest salary in each department.
Thank you! Connect with me on LinkedIn for more updates: @Aditya Raja