1. Basic SQL Queries
What is SQL?
- SQL (Structured Query Language) is a standard programming language for managing and manipulating relational databases.
What are the different types of SQL commands?
- DDL (Data Definition Language): Commands like
CREATE
,ALTER
,DROP
. - DML (Data Manipulation Language): Commands like
SELECT
,INSERT
,UPDATE
,DELETE
. - DCL (Data Control Language): Commands like
GRANT
,REVOKE
. - TCL (Transaction Control Language): Commands like
COMMIT
,ROLLBACK
.
- DDL (Data Definition Language): Commands like
What is a primary key?
- A primary key is a unique identifier for a record in a table. It cannot contain NULL values.
What is a foreign key?
- A foreign key is a field in one table that uniquely identifies a row of another table. It establishes a relationship between the two tables.
What is normalization?
- Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity.
What are the different types of joins?
- INNER JOIN: Returns records with matching values in both tables.
- LEFT JOIN: Returns all records from the left table and matched records from the right table.
- RIGHT JOIN: Returns all records from the right table and matched records from the left table.
- FULL JOIN: Returns all records when there is a match in either left or right table.
What is a subquery?
- A subquery is a query nested inside another SQL query. It can return a single value or a set of values.
What is a view?
- A view is a virtual table based on the result of a SQL query. It does not store data physically.
What is an index?
- An index is a database object that improves the speed of data retrieval operations on a database table.
What is a stored procedure?
- A stored procedure is a precompiled collection of one or more SQL statements that can be executed as a single unit.
2. Intermediate SQL Concepts
What is a trigger?
- A trigger is a special type of stored procedure that automatically runs when a specific event occurs in the database.
Explain ACID properties.
- Atomicity: Ensures that all operations within a transaction are completed; if not, the transaction is aborted.
- Consistency: Ensures that a transaction brings the database from one valid state to another.
- Isolation: Ensures that transactions are executed in isolation from each other.
- Durability: Ensures that once a transaction has been committed, it will remain so even in the event of a system failure.
What are aggregate functions?
- Aggregate functions perform calculations on multiple values and return a single value. Examples include
COUNT()
,SUM()
,AVG()
,MAX()
, andMIN()
.
- Aggregate functions perform calculations on multiple values and return a single value. Examples include
What is the difference between UNION and UNION ALL?
UNION
combines the result sets of two or more SELECT statements and removes duplicates, whileUNION ALL
includes all duplicates.
What is a common table expression (CTE)?
- A CTE is a temporary result set defined within the execution scope of a single
SELECT
,INSERT
,UPDATE
, orDELETE
statement.
- A CTE is a temporary result set defined within the execution scope of a single
What is the difference between a clustered index and a non-clustered index?
- A clustered index determines the physical order of data in a table, while a non-clustered index is a separate structure that points to the physical data.
How do you delete duplicate rows in a table?
- You can delete duplicates by using a Common Table Expression (CTE) with the
ROW_NUMBER()
function to identify duplicates and then delete them.
- You can delete duplicates by using a Common Table Expression (CTE) with the
What is a schema?
- A schema is a collection of database objects, including tables, views, and indexes, associated with a particular database user.
What are stored functions?
- Stored functions are similar to stored procedures but return a single value and can be used in SQL expressions.
What is the purpose of the GROUP BY clause?
- The
GROUP BY
clause is used to arrange identical data into groups. It is often used with aggregate functions.
- The
3. Advanced SQL Concepts
What are window functions?
- Window functions perform calculations across a set of table rows related to the current row, providing insights like running totals.
Explain the difference between a correlated subquery and a non-correlated subquery.
- A correlated subquery depends on the outer query for its values, while a non-correlated subquery can be executed independently.
What is the purpose of the HAVING clause?
- The
HAVING
clause is used to filter records after theGROUP BY
operation has been performed, unlikeWHERE
, which filters records before aggregation.
- The
How can you optimize SQL queries?
- Optimizing queries can involve creating indexes, avoiding SELECT *, using WHERE clauses to filter results, and analyzing execution plans.
What are the different types of constraints?
- NOT NULL: Ensures a column cannot have a NULL value.
- UNIQUE: Ensures all values in a column are different.
- CHECK: Ensures that all values in a column satisfy a specific condition.
- DEFAULT: Provides a default value for a column when none is specified.
What is a transaction?
- A transaction is a sequence of one or more SQL operations that are executed as a single unit of work.
What is the purpose of the EXPLAIN statement?
- The
EXPLAIN
statement provides information about how SQL statements are executed, including how tables are joined and the use of indexes.
- The
What are materialized views?
- Materialized views store the results of a query physically, improving performance for complex queries that do not change often.
What is SQL injection?
- SQL injection is a security vulnerability that allows an attacker to interfere with the queries made to a database.
How can you prevent SQL injection?
- Use prepared statements, parameterized queries, and stored procedures to safeguard against SQL injection attacks.
4. SQL Functions and Operators
What are scalar functions?
- Scalar functions return a single value based on the input value(s). Examples include
UPPER()
,LOWER()
, andLEN()
.
- Scalar functions return a single value based on the input value(s). Examples include
What is a date function?
- Date functions manipulate date values. Examples include
GETDATE()
,DATEDIFF()
, andDATEADD()
.
- Date functions manipulate date values. Examples include
What is the COALESCE function?
COALESCE
returns the first non-null value in a list of expressions.
What is the difference between CHAR and VARCHAR?
CHAR
is a fixed-length data type, whileVARCHAR
is variable-length and can store strings of varying lengths.
How do you find the current database user?
- You can use the SQL command
SELECT USER();
orSELECT CURRENT_USER();
depending on the SQL dialect.
- You can use the SQL command
What are string functions in SQL?
- String functions manipulate string values, including
CONCAT()
,SUBSTRING()
, andTRIM()
.
- String functions manipulate string values, including
What is the RANK() function?
RANK()
is a window function that assigns a rank to each row within a partition of a result set, with gaps in ranking for ties.
What is a CASE statement?
- The
CASE
statement provides conditional logic in SQL queries, allowing different outputs based on certain conditions.
- The
How can you convert data types in SQL?
- You can use the
CAST()
orCONVERT()
functions to change data types in SQL.
- You can use the
What is the purpose of the EXISTS operator?
- The
EXISTS
operator checks for the existence of rows returned by a subquery, returning true if rows exist.
- The
5. Database Design and Optimization
What is denormalization?
- Denormalization is the process of intentionally introducing redundancy into a database design to improve read performance.
What is a star schema?
- A star schema is a type of database schema that consists of a central fact table surrounded by dimension tables.
What is a snowflake schema?
- A snowflake schema is a more complex version of a star schema, where dimension tables are normalized into multiple related tables.
How do you design a database for a new application?
- Start by gathering requirements, identify entities and relationships, create an ER diagram, define the schema, and normalize the database.
What is the purpose of indexing?
- Indexing speeds up the retrieval of rows from a database table, improving query performance.
How do you identify performance bottlenecks in SQL queries?
- Analyze execution plans, check for long-running queries, monitor system resources, and look for missing indexes.
What is data integrity?
- Data integrity ensures the accuracy and consistency of data over its lifecycle through constraints and validation rules.
What is referential integrity?
- Referential integrity ensures that relationships between tables remain consistent, preventing orphaned records.
How do you implement cascading updates/deletes?
- You can implement cascading updates or deletes using foreign key constraints with the
ON UPDATE CASCADE
orON DELETE CASCADE
options.
- You can implement cascading updates or deletes using foreign key constraints with the
What are the advantages of using stored procedures?
- Stored procedures encapsulate business logic, enhance security, reduce network traffic, and improve performance.
6. SQL in Practice
Write a query to find the second highest salary.
How do you count the number of rows in a table?
Write a SQL query to find all employees who joined after January 1, 2020.
How can you retrieve unique records from a table?
Write a query to concatenate first and last names.
How do you update a specific record in a table?
Write a query to delete employees who have not worked for more than a year.
How can you retrieve the maximum salary from a table?
Write a SQL query to retrieve records with a specific condition.
How do you perform a self-join?
7. Advanced SQL Use Cases
What is the purpose of the GROUP_CONCAT function?
GROUP_CONCAT
concatenates values from multiple rows into a single string, typically used in aggregate queries.
How do you implement pagination in SQL?
What is a temporary table?
- A temporary table is a table that is created and used temporarily within a session, automatically dropped at the end of the session.
How do you create a trigger?
What is the purpose of the TRANSACTION statement?
- The
TRANSACTION
statement groups a set of SQL commands to ensure they are executed as a single unit, maintaining data integrity.
- The
How do you create a recursive CTE?
Write a SQL query to find the total sales per product.
What is the difference between TRUNCATE and DELETE?
TRUNCATE
removes all rows from a table without logging individual row deletions, whileDELETE
logs each row deletion and can include a WHERE clause.
How can you create a backup of a database?
- You can use the
BACKUP DATABASE
command in SQL Server ormysqldump
in MySQL.
- You can use the
What is the purpose of the SET NOCOUNT ON statement?
SET NOCOUNT ON
prevents the message indicating the number of affected rows from being returned, which can improve performance in stored procedures.
8. SQL Best Practices
What are the best practices for writing SQL queries?
- Use descriptive names, avoid SELECT *, use joins instead of subqueries where possible, and always test queries for performance.
How do you ensure data security in SQL?
- Implement user roles and permissions, use parameterized queries, and encrypt sensitive data.
What is the significance of database backups?
- Database backups are essential for data recovery in case of corruption, data loss, or hardware failures.
What is the difference between a logical and physical data model?
- A logical data model outlines the structure of the data without concern for how it will be implemented physically, while a physical data model includes details about how data is stored.
What is data warehousing?
- Data warehousing is the process of collecting and managing data from various sources to provide meaningful business insights.
How do you handle error management in SQL?
- Use
TRY...CATCH
blocks in T-SQL to handle exceptions and log errors.
- Use
What are some common performance tuning techniques?
- Optimize queries, use proper indexing, avoid unnecessary columns in SELECT statements, and analyze execution plans.
What is database partitioning?
- Database partitioning is the process of dividing a database into smaller, more manageable pieces, improving performance and maintainability.
How can you manage large datasets in SQL?
- Use indexing, partitioning, and archiving older data to improve performance with large datasets.
What is a database migration?
- Database migration involves moving data from one database to another or upgrading to a newer version of a database system.
9. SQL and Application Integration
How do you connect to a database from an application?
- Use a database driver or ORM (Object-Relational Mapping) framework in the application language to establish a connection.
What is ORM?
- ORM (Object-Relational Mapping) is a programming technique that converts data between incompatible type systems in object-oriented programming languages.
How can you secure database connections in an application?
- Use secure connection strings, implement SSL/TLS for connections, and limit user privileges.
What is the role of an API in database interactions?
- An API (Application Programming Interface) allows applications to communicate with the database, executing SQL queries and retrieving results.
How do you manage database schema changes in applications?
- Use migration tools that track and apply schema changes incrementally and manage version control for database changes.
What are some common database design patterns?
- Common patterns include repository pattern, unit of work pattern, and data mapper pattern.
How do you handle concurrency in SQL databases?
- Implement locking mechanisms, isolation levels, and optimistic concurrency control to manage concurrent access to data.
What is the significance of data types in database design?
- Choosing appropriate data types ensures optimal storage efficiency, data integrity, and performance.
How can you handle large result sets in an application?
- Use pagination techniques, lazy loading, or caching to manage large datasets efficiently.
What is the role of a database administrator (DBA)?
- A DBA is responsible for managing, backing up, and ensuring the availability and performance of the database systems.
10. SQL in Industry Applications
How is SQL used in data analysis?
- SQL is used to query large datasets, aggregate data, and generate reports for insights and decision-making.
What role does SQL play in business intelligence?
- SQL is used to extract, transform, and load (ETL) data into data warehouses, enabling analytics and reporting.
How can SQL be used in machine learning?
- SQL can be used to preprocess data, query training datasets, and integrate with machine learning platforms for model training.
What is the significance of data governance in SQL databases?
- Data governance ensures data quality, compliance, and management practices are in place for data handling.
How does SQL interact with cloud databases?
- SQL can be used to query and manage cloud-based databases like AWS RDS, Azure SQL Database, and Google Cloud SQL.
What is the difference between OLTP and OLAP?
- OLTP (Online Transaction Processing) systems are optimized for transaction-oriented applications, while OLAP (Online Analytical Processing) systems are designed for complex queries and data analysis.
What are data lakes, and how does SQL fit in?
- Data lakes store vast amounts of raw data in its native format. SQL can be used to query structured data within a data lake.
How can SQL be utilized in e-commerce applications?
- SQL manages product catalogs, customer data, orders, and transactions in e-commerce systems.
What is the impact of NoSQL on SQL databases?
- NoSQL databases provide flexibility for unstructured data and scalability, leading traditional SQL databases to adapt and coexist.
How do you see the future of SQL in data management? - SQL will continue to evolve with support for new data types, integration with big data technologies, and enhanced capabilities for cloud-native environments.