Top 50+ SQL Asked Question in Interview
- Data Definition Language (DDL) – to define and modify the structure of a database.
- Data Manipulation Language (DML) – to access, manipulate, and modify data in a database.
- Data Control Language (DCL) – to control user access to the data in the database and give or revoke privileges to a specific user or a group of users.
- Transaction Control Language (TCL) – to control transactions in a database.
- Data Query Language (DQL) – to perform queries on the data in a database to retrieve the necessary information from it.
Key Differences:
- DBMS: In a DBMS, data is stored in files, which can be hierarchical, network, or object-oriented.
- RDBMS: RDBMS stores data in tables with rows and columns. Tables can have relationships defined between them using keys.
- DBMS: DBMS does not enforce referential integrity by default.
- RDBMS: RDBMS enforces referential integrity through foreign key constraints, ensuring data consistency.
- DBMS: DBMS may support its own query language.
- RDBMS: RDBMS typically uses SQL (Structured Query Language) to interact with the database.
- DBMS: DBMS may not be as scalable as RDBMS due to the lack of normalization and relationships.
- RDBMS: RDBMS offers better scalability through normalization and efficient data retrieval using SQL queries.
- DBMS: Performance may vary based on the data structure and complexity.
- RDBMS: RDBMS, with its relational model and optimized queries, often provides better performance for complex queries involving multiple tables.
- DBMS: Data consistency needs to be managed at the application level.
- RDBMS: RDBMS ensures data consistency through ACID properties (Atomicity, Consistency, Isolation, Durability).
SQL is a standard language for retrieving and manipulating structured databases. On the contrary, MySQL is a relational database management system, like SQL Server, Oracle or IBM DB2, that is used to manage SQL databases.
- Standardization: SQL is an ANSI (American National Standards Institute) and ISO (International Organization for Standardization) standardized language, ensuring consistency across database systems.
- Functionality: SQL provides a set of commands (e.g., SELECT, INSERT, UPDATE, DELETE) to perform various operations on databases.
- Portability: SQL queries can be executed on different database management systems (DBMS) that support SQL, making it versatile and widely adopted.
- Database Management System: MySQL is a specific implementation of a database management system that utilizes SQL for querying and managing databases.
- Features: MySQL offers features like transactions, indexing, and stored procedures to enhance database functionality.
- Scalability: MySQL is known for its scalability, making it suitable for small to large-scale applications.
- SQL vs. MySQL: SQL is a language used to interact with databases, while MySQL is a specific RDBMS that implements SQL.
- Flexibility: SQL is a standard language that can be used with various DBMS, whereas MySQL is a specific database system.
- Open Source: MySQL is open-source and free to use, while SQL is a language standard without a specific implementation.
Constraints are used to specify the rules concerning data in the table. It can be applied for single or multiple fields in an SQL table during the creation of the table or after creating using the ALTER TABLE command. The constraints are:
- NOT NULL - Restricts NULL value from being inserted into a column.
- CHECK - Verifies that all values in a field satisfy a condition.
- DEFAULT - Automatically assigns a default value if no value has been specified for the field.
- UNIQUE - Ensures unique values to be inserted into the field.
- INDEX - Indexes a field providing faster retrieval of records.
- PRIMARY KEY - Uniquely identifies each record in a table.
- FOREIGN KEY - Ensures referential integrity for a record in another table.
CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, EmployeeName VARCHAR(50) UNIQUE, DepartmentID INT, Salary DECIMAL(10, 2) );
In this example, the EmployeeName column has a UNIQUE constraint, meaning that each employee's name must be unique in the Employees table. If an attempt is made to insert a new row with a duplicate EmployeeName, the database will raise an error, ensuring data consistency.
Overall, the UNIQUE constraint plays a vital role in maintaining the quality and accuracy of data stored in a database by enforcing uniqueness at the column level.
CREATE TABLE Orders ( OrderID int PRIMARY KEY, ProductID int, OrderDate date, FOREIGN KEY (ProductID) REFERENCES Products(ProductID) );
In this example, the Orders table has a Foreign Key ProductID that references the ProductID column in the Products table. This ensures that every ProductID in the Orders table must exist in the Products table or be NULL, maintaining the integrity of the relationship between the two tables.
Primary Key
- Primary key cannot have a NULL value.
- Each table can have only one primary key.
- By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index.
- Primary key can be related with another table's as a Foreign Key.
- We can generated ID automatically with the help of Auto Increment field. Primary key supports Auto Increment value.
Unique Key
- Unique Constraint may have a NULL value.
- Each table can have more than one Unique Constraint.
- By default, Unique key is a unique non-clustered index.
- Unique Constraint can not be related with another table's as a Foreign Key.
- Unique Constraint doesn't supports Auto Increment value.
Foreign Key
- Foreign key is a field in the table that is primary key in another table.
- Foreign key can accept multiple null value.
- Foreign key do not automatically create an index, clustered or non-clustered. You can manually create an index on foreign key.
- We can have more than one foreign key in a table.
- There are actual advantages to having a foreign key be supported with a clustered index, but you get only one per table. What's the advantage? If you are selecting the parent plus all child records, you want the child records next to each other. This is easy to accomplish using a clustered index.
A database index is a data structure that provides a quick lookup of data in a column or columns of a table. It enhances the speed of operations accessing data from a database table at the cost of additional writes and memory to maintain the index data structure.
CREATE INDEX index_name /* Create Index */
ON table_name (column_1, column_2);
DROP INDEX index_name; /* Drop Index */
There are different types of indexes that can be created for different purposes:
- Unique and Non-Unique Index:
Unique indexes are indexes that help maintain data integrity by ensuring that no two rows of data in a table have identical key values. Once a unique index has been defined for a table, uniqueness is enforced whenever keys are added or changed within the index.
CREATE UNIQUE INDEX myIndex
ON students (enroll_no);
Non-unique indexes, on the other hand, are not used to enforce constraints on the tables with which they are associated. Instead, non-unique indexes are used solely to improve query performance by maintaining a sorted order of data values that are used frequently.
- Clustered and Non-Clustered Index:
Clustered indexes are indexes whose order of the rows in the database corresponds to the order of the rows in the index. This is why only one clustered index can exist in a given table, whereas, multiple non-clustered indexes can exist in the table.
The only difference between clustered and non-clustered indexes is that the database manager attempts to keep the data in the database in the same order as the corresponding keys appear in the clustered index.
Clustering indexes can improve the performance of most query operations because they provide a linear-access path to data stored in the database.
- Physical Sorting: A Clustered index determines the physical order of data rows in a table based on the indexed column(s). It rearranges the actual data pages to match the index order.
- Unique Nature: By default, a Clustered index is unique, meaning that each row in the table is uniquely identified by the Clustered index key.
- Storage: As the data rows are stored in the order of the Clustered index, only one Clustered index is allowed per table.
- Performance: Clustered indexes are beneficial for range queries and queries that require sorting or grouping based on the indexed column.
- Logical Sorting: A Non-clustered index does not alter the physical order of data rows. Instead, it creates a separate structure that holds the index key values along with pointers to the actual data rows.
- Non-Unique: Non-clustered indexes do not enforce uniqueness by default, allowing duplicate values in the indexed column(s).
- Storage: Multiple Non-clustered indexes can be created on a single table without affecting the physical order of data.
- Performance: Non-clustered indexes are advantageous for queries that involve joins, sorting, or filtering based on columns other than the indexed one.
- Data Order: Clustered indexes dictate the physical order of data, while Non-clustered indexes do not impact the data's physical arrangement.
- Uniqueness: Clustered indexes are unique, whereas Non-clustered indexes can contain duplicate values.
- Storage: Clustered indexes alter the data storage, allowing only one per table, whereas Non-clustered indexes create separate structures and permit multiple indexes.
- Performance: The choice between Clustered and Non-clustered indexes depends on the query patterns and the nature of operations performed on the data.
- Entity Integrity: This aspect ensures that each row or record in a table is uniquely identified by a primary key. It prevents duplicate records and enforces uniqueness.
- Referential Integrity: This ensures the relationships between tables are maintained correctly. For example, if a record in one table references a record in another table, the foreign key constraint ensures that the referenced record exists.
- Domain Integrity: It enforces valid data types, formats, and ranges for columns. This prevents incorrect data from being inserted into the database.
- User-defined Integrity: This involves custom rules and constraints defined by the user to maintain specific data requirements that are not covered by other integrity types.
- Check Constraints: These constraints define rules that data must follow to maintain integrity. For example, limiting the values that can be inserted into a column.
- Trigger-based Integrity: Triggers can be used to enforce complex integrity rules or actions when certain events occur in the database.
- SELECT: Specifies the columns to retrieve data from.
- FROM: Specifies the table(s) from which to retrieve data.
- WHERE: Filters data based on specified conditions.
- GROUP BY: Groups rows sharing a common value.
- HAVING: Filters groups based on specified conditions.
- ORDER BY: Sorts the result set based on specified columns.
- JOIN: Combines rows from two or more tables based on a related column between them.
SELECT first_name, last_name FROM employees WHERE salary > 50000;
In this example:
SELECT first_name, last_name specifies the columns to retrieve.
FROM employees indicates the table from which to retrieve data.
WHERE salary > 50000 filters the data based on the condition that the salary is greater than $50,000.
By executing this query, we would retrieve the first and last names of employees meeting the specified criteria.
Queries are fundamental to interacting with databases, enabling users to extract valuable insights and information efficiently. Understanding how to construct queries empowers users to manipulate and retrieve data according to their requirements.
- The DROP statement is used to delete an entire table from the database.
- When you execute a DROP statement, it removes the table structure along with all the data, indexes, constraints, and triggers associated with that table.
- It is a DDL (Data Definition Language) command and cannot be rolled back. Once a table is dropped, it is gone permanently.
Drop table table_Name
TRUNCATE Statement:
- The TRUNCATE statement is used to delete all the rows from a table, but it retains the table structure.
- When you execute a TRUNCATE statement, it removes all the data from the table but keeps the table itself, along with its columns, constraints, indexes, and triggers.
- It is a DDL (Data Definition Language) command and also cannot be rolled back. Once data is truncated, it is not recoverable.
The syntax for the TRUNCATE statement is as follows:
truncate table table_Name
Key Differences:
- Functionality: DROP deletes the entire table structure, while TRUNCATE only removes the data inside the table.
- Rollback: Neither DROP nor TRUNCATE can be rolled back, but TRUNCATE allows you to reuse the table structure.
- Performance: TRUNCATE is faster than DROP since it does not log individual row deletions.
- Permissions: DROP requires DROP permission on the table, while TRUNCATE requires DELETE permission on the table.
The DELETE command deletes only the rows from the table based on the condition given in the where clause or deletes all the rows from the table if no condition is specified. But it does not free the space containing the table.
An aggregate function performs operations on a collection of values to return a single scalar value. Aggregate functions are often used with the GROUP BY and HAVING clauses of the SELECT statement. Following are the widely used SQL aggregate functions:
- AVG() - Calculates the mean of a collection of values.
- COUNT() - Counts the total number of records in a specific table or view.
- MIN() - Calculates the minimum of a collection of values.
- MAX() - Calculates the maximum of a collection of values.
- SUM() - Calculates the sum of a collection of values.
- FIRST() - Fetches the first element in a collection of values.
- LAST() - Fetches the last element in a collection of values.
Note: All aggregate functions described above ignore NULL values except for the COUNT function.
A scalar function returns a single value based on the input value. Following are the widely used SQL scalar functions:
- LEN() - Calculates the total length of the given field (column).
- UCASE() - Converts a collection of string values to uppercase characters.
- LCASE() - Converts a collection of string values to lowercase characters.
- MID() - Extracts substrings from a collection of string values in a table.
- CONCAT() - Concatenates two or more strings.
- RAND() - Generates a random collection of numbers of a given length.
- ROUND() - Calculates the round-off integer value for a numeric field (or decimal point values).
- NOW() - Returns the current date & time.
- FORMAT() - Sets the format to display a collection of values.
CREATE FUNCTION dbo.SquareFunction (@num INT) RETURNS INT AS BEGIN RETURN @num * @num; END
2- Table-Valued Functions: These functions return a table as the output. They can be used in the FROM clause of a SELECT statement. For instance, a function that returns a table of employees in a specific department:
CREATE FUNCTION dbo.GetEmployeesByDepartment (@deptId INT) RETURNS TABLE AS RETURN ( SELECT * FROM Employees WHERE DepartmentID = @deptId )
3- Inline Table-Valued Functions: These functions return a table variable directly. They are more efficient than multi-statement table-valued functions. An example of an inline table-valued function:
CREATE FUNCTION dbo.GetEmployeesByDepartment (@deptId INT) RETURNS TABLE AS RETURN SELECT * FROM Employees WHERE DepartmentID = @deptId
Benefits of User-defined Functions:
- Code Reusability: Functions can be called from multiple queries, reducing code duplication.
- Modularity: Logic encapsulation allows for easier maintenance and updates.
- Performance: Properly designed functions can enhance query performance by reducing repetitive code execution.
Creating empty tables with the same structure can be done smartly by fetching the records of one table into a new table using the INTO operator while fixing a WHERE clause to be false for all records. Hence, SQL prepares the new table with a duplicate structure to accept the fetched records but since no records get fetched due to the WHERE clause in action, nothing is inserted into the new table.
SELECT * INTO Order_copy
FROM Order WHERE 1 = 2
- Precompiled SQL Code: Stored Procedures are precompiled, which means the SQL statements are parsed, compiled, and optimized before execution, leading to faster query processing.
- Parameterized: Stored Procedures can accept input parameters, enabling dynamic data processing based on user-defined values.
- Encapsulation: They encapsulate complex business logic within the database, promoting data integrity and security by controlling access to the underlying tables.
- Reusability: Stored Procedures can be called multiple times from different parts of an application, reducing code duplication and promoting maintainability.
- Transaction Management: They support transaction management, allowing multiple SQL statements to be executed as a single unit of work, ensuring data consistency.
CREATE PROCEDURE sp_GetEmployeeDetails @EmployeeID INT AS BEGIN SELECT * FROM Employees WHERE EmployeeID = @EmployeeID; END
Benefits of Stored Procedures:
- Improved Performance: Reduced network traffic and optimized query execution.
- Enhanced Security: Controlled access to data and prevention of SQL injection attacks.
- Code Reusability: Centralized logic that can be reused across applications.
- Ease of Maintenance: Changes made in a Stored Procedure reflect across all applications using it.
- Modularity: Encapsulation of logic for better organization and management.
- Used for performing a set of SQL statements and can contain programming logic.
- Can execute multiple SQL queries and perform complex operations.
- Ideal for tasks requiring conditional processing and looping.
- Designed to return a single value or a table.
- Cannot perform DML operations like INSERT, UPDATE, or DELETE.
- Suitable for computations and returning specific values.
- Does not have a return type.
- Can return multiple result sets using SELECT statements.
- Must have a return type defined.
- Returns a single value or a table.
- Called using EXECUTE or EXEC command.
- Can be used to perform operations and return result sets.
- Invoked in SQL queries like a table or a scalar value.
- Can be part of a SELECT statement or used in WHERE clauses.
- Can contain transaction management commands like COMMIT and ROLLBACK.
- Used for handling complex transaction scenarios.
- Cannot control transactions.
- Transactions are managed by the calling process.
- Supports TRY...CATCH blocks for error handling.
- Can handle exceptions within the procedure.
- Limited error handling capabilities.
- Errors propagate to the calling environment.
- Supports recursion.
- Can call itself within the procedure.
- Limited support for recursion.
- Recursive functions are less common and have restrictions.
- Uniqueness: The combination of values in the composite key must be unique across all records in the table.
- Order: The order of columns in a composite key is crucial, as changing the sequence of columns can result in a different key.
- Data Integrity: Composite keys help maintain data integrity by ensuring that no duplicate records can exist based on the combined values of the key columns.
- Performance: While composite keys offer uniqueness, they can impact performance, especially in large tables, as the database needs to index and search based on multiple columns.
CREATE TABLE Employees ( EmployeeID INT, DepartmentID INT, PRIMARY KEY (EmployeeID, DepartmentID) );
In this example, the Employees table has a composite key consisting of EmployeeID and DepartmentID. Together, these two columns form a unique identifier for each employee within a department.
Benefits of Composite Keys:
- Complex Relationships: Composite keys are useful when dealing with complex relationships between entities that require multiple attributes for identification.
- Normalization: In some cases, using a composite key can help in normalizing the database structure by reducing redundancy.
- Maintaining Data Integrity: By enforcing uniqueness across multiple columns, composite keys ensure data integrity and prevent inconsistencies.
SELECT ISNULL(column_name, 'N/A') AS new_column_name FROM table_name;
In this example, if column_name is NULL, it will be replaced with 'N/A'.
COALESCE Function
The COALESCE function is an ANSI standard function supported by most relational database management systems. It returns the first non-NULL expression among its arguments. It can take multiple arguments and returns the first non-NULL value encountered. Here is an example of how COALESCE is used:-
SELECT COALESCE(column_name1, column_name2, 'N/A') AS new_column_name FROM table_name;
In this example, COALESCE will return column_name1 if it is not NULL. If column_name1 is NULL, it will return column_name2. If both column_name1 and column_name2 are NULL, it will return 'N/A'.
Key Differences
- Portability: ISNULL is specific to SQL Server, while COALESCE is an ANSI standard function supported by many database systems.
- Number of Arguments: ISNULL takes only two arguments, while COALESCE can take multiple arguments.
- Return Value: ISNULL returns the data type of the first argument, while COALESCE returns the data type of the result expression.
- Scope: Local temporary tables are only visible to the current session. They are automatically dropped when the session that created them ends.
- Naming Convention: Local temporary tables are prefixed with a single pound sign (#). For example, #myLocalTempTable.
- Usage: These tables are useful when you need temporary storage that is specific to a single session and should not interfere with other sessions.
CREATE TABLE #myLocalTempTable ( ID INT, Name VARCHAR(50) );
Global Temporary Tables :
Scope: Global temporary tables are visible to all sessions. They are dropped when the last session using the table ends.
Naming Convention: Global temporary tables are prefixed with a double pound sign (##). For example, ##myGlobalTempTable.
Usage: Global temporary tables are handy when you need temporary storage that can be shared across multiple sessions.
CREATE TABLE ##myGlobalTempTable ( ID INT, Name VARCHAR(50) );
local temporary tables are session-specific and are automatically dropped when the session ends, while global temporary tables are visible across sessions and are dropped when the last session using them ends. Choose the appropriate type based on your specific requirements for temporary data storage in SQL.
- Logical Grouping: Schemas help in logically grouping database objects together. This organization aids in better management and maintenance of the database.
- Security: Schemas play a crucial role in managing security within a database. By assigning permissions at the schema level, you can control access to multiple objects at once.
- Default Schema: Each user in SQL Server has a default schema. When a user creates an object without specifying a schema, it is created in the user's default schema.
- Schema Ownership: Schemas are owned by a database principal, usually a user or role. The owner has control over the objects within that schema.
- Cross-schema References: Schemas allow for referencing objects across schemas. This enables better organization and separation of concerns within a database.
SELECT CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... ELSE default_result END FROM your_table;
Here's a breakdown of the components:
- WHEN condition THEN result: This part checks if a condition is met and returns a specific result if true.
- ELSE default_result: If none of the conditions are met, the ELSE clause specifies the default result to return.
Let's illustrate this with an example. Suppose we have a table Employees with columns EmployeeID, FirstName, and LastName. We want to create a new column FullName that concatenates the first and last names.
SELECT EmployeeID, FirstName, LastName, CASE WHEN FirstName IS NOT NULL AND LastName IS NOT NULL THEN FirstName + ' ' + LastName ELSE 'Name Not Provided' END AS FullName FROM Employees;
In this example, the CASE statement checks if both the FirstName and LastName are not NULL. If they are not NULL, it concatenates them with a space in between. Otherwise, it returns 'Name Not Provided'.
The CASE statement can also be used in the WHERE clause, ORDER BY clause, and even nested within another CASE statement for more complex logic
- Data Model: SQL databases use a tabular structure with predefined schemas, while NoSQL databases offer various data models like document-based, key-value, column-family, and graph.
- Scalability: NoSQL databases are horizontally scalable, enabling them to handle large datasets and distributed systems more efficiently than SQL databases.
- Flexibility: NoSQL databases provide flexibility in data storage, allowing for dynamic schema changes and accommodating diverse data types.
- Complexity: SQL databases are suitable for complex queries and transactions due to their structured nature, while NoSQL databases excel in handling simple queries on large datasets.
- Consistency: SQL databases prioritize data consistency with ACID properties, whereas NoSQL databases may sacrifice consistency for availability and partition tolerance (CAP theorem).
Key Aspects of Database Black Box Testing:
- Data Integrity Testing: Verifying that data stored in the database remains accurate and consistent after various operations.
- Data Validity Testing: Ensuring that the data stored in the database meets predefined constraints and validation rules.
- Performance Testing: Evaluating the database's performance under different load conditions to identify bottlenecks and optimize queries.
- Security Testing: Assessing the database's vulnerability to unauthorized access, SQL injection, and other security threats.
- Concurrency Testing: Testing the database's ability to handle multiple transactions simultaneously without data corruption.
Benefits of Database Black Box Testing:
- Independence: Testers do not need to understand the internal logic of the database, allowing for unbiased testing.
- Comprehensive Testing: Covers a wide range of scenarios without being limited by internal implementation details.
- Error Detection: Helps in identifying issues related to data integrity, performance, security, and scalability.
- Risk Mitigation: Reduces the risk of deploying faulty database changes that could impact the overall system.
-- Example: Testing a stored procedure for data integrity CREATE PROCEDURE dbo.UpdateEmployeeSalary @EmployeeID INT, @NewSalary DECIMAL(10, 2) AS BEGIN UPDATE Employees SET Salary = @NewSalary WHERE EmployeeID = @EmployeeID END
In this SQL example, a stored procedure UpdateEmployeeSalary is tested without knowing the internal implementation details. Test cases can be designed to validate the correctness of salary updates without accessing the procedure's source code.
- One-to-One Relationship: A single record in one table is related to only one record in another table.
- One-to-Many Relationship: A single record in one table can be related to multiple records in another table.
- Many-to-Many Relationship: Multiple records in one table can be related to multiple records in another table.
- Data Integrity: By reducing redundancy and dependency, normalization helps maintain data integrity. Updates, inserts, and deletes are less likely to cause anomalies, ensuring data consistency.
- Efficient Use of Storage: Normalization optimizes storage space by eliminating redundant data. Smaller tables require less storage, leading to efficient use of resources.
- Improved Query Performance: Normalized databases often perform better in terms of query speed. Smaller tables with fewer columns can be queried more efficiently.
- Simplified Updates: With normalized data, updates only need to be made in one place, reducing the chances of inconsistencies across the database.
- Scalability: Normalized databases are more scalable as they can handle growth and changes in data requirements more effectively. Adding new data is easier without impacting existing data.
- Easier Maintenance: Maintenance tasks such as backups, indexing, and data manipulation become more straightforward in normalized databases due to the organized structure.
- Reduction of Redundancy: Normalization eliminates redundant data, which not only saves storage space but also reduces the risk of inconsistencies that can arise from duplicate information.
- Smoother Database Design: Following normalization principles leads to a well-structured database design that is easier to understand and maintain over time.
- Transactional Nature: OLTP systems handle a large number of short online transactions, such as inserting, updating, and deleting small amounts of data in real-time.
- Concurrency Control: OLTP systems support multiple users concurrently accessing and modifying data without compromising data integrity. Techniques like locking mechanisms are used to manage concurrent access.
- Data Normalization: Data in OLTP systems is typically normalized to reduce redundancy and improve data consistency. This ensures efficient storage and retrieval of data.
- Indexing: Indexes are crucial in OLTP systems to speed up data retrieval operations, especially in scenarios where frequent querying of specific data subsets is required.
- ACID Properties: OLTP systems adhere to the ACID (Atomicity, Consistency, Isolation, Durability) properties to ensure reliable and secure transaction processing.
-- Example: Retrieve all orders for a specific customer SELECT OrderID, OrderDate, TotalAmount FROM Orders WHERE CustomerID = '123'
In summary, OLTP systems are designed for operational efficiency, ensuring that day-to-day transactions are processed swiftly and accurately. They form the backbone of applications like e-commerce platforms, banking systems, and airline reservations, where real-time data processing is critical for business operations.
It’s a very reliable database management system, with more than two decades of community work to thank for its high levels of resiliency, integrity, and accuracy. Many online, mobile, geospatial, and analytics applications utilise PostgreSQL as their primary data storage or data warehouse.
SELECT name, score, RANK() OVER (ORDER BY score DESC) AS rank FROM students;
DENSE_RANK() Function:
On the other hand, the DENSE_RANK() function also assigns a unique rank to each distinct row in the result set. However, unlike RANK(), DENSE_RANK() does not leave gaps in the ranking sequence when there are ties. It ensures that the ranks are consecutive without any gaps, even if there are tied rows.
Here is an example of how the DENSE_RANK() function works:
SELECT name, score, DENSE_RANK() OVER (ORDER BY score DESC) AS dense_rank FROM students;
Key Differences:
- Handling Ties: RANK() leaves gaps in the ranking sequence for tied rows, while DENSE_RANK() assigns consecutive ranks without gaps.
- Sequential vs. Non-Sequential: RANK() produces a sequential ranking that may have gaps, whereas DENSE_RANK() provides a non-sequential ranking without gaps.
- Use Cases: RANK() is useful when you want to distinguish tied rows distinctly, while DENSE_RANK() is preferred when you need a continuous ranking without gaps.
SELECT * FROM users WHERE username = '{input_username}' AND password = '{input_password}';
If the input fields are not sanitized, an attacker could input ' OR '1'='1 as the username and password, resulting in the following query:
SELECT * FROM users WHERE username = '' OR '1'='1' AND password = '' OR '1'='1';
This modified query always returns true, allowing the attacker to bypass authentication and gain unauthorized access.
Implications of SQL Injection
SQL Injection poses significant risks to the security and integrity of a database-driven application. Some of the potential consequences include:
- Unauthorized access to sensitive data
- Data manipulation or deletion
- Extraction of confidential information
- Complete compromise of the database server
- Execution of arbitrary commands on the database
Mitigating SQL Injection
To prevent SQL Injection attacks, developers should implement the following best practices:
- Use parameterized queries or prepared statements to sanitize user input
- Avoid dynamic SQL construction based on user input
- Implement input validation and encoding
- Limit database user permissions to reduce the impact of successful attacks
- Regularly update and patch the application and database software
- NULL represents the absence of a value or an unknown value.
- It is not the same as zero or an empty string.
- When a field contains a NULL value, it means the value is missing or unknown.
- Comparing NULL to any other value, including another NULL, using standard comparison operators like '=' or '<>' will result in an unknown or NULL outcome.
- Zero is a numerical value that represents the number zero.
- It is a specific value and is different from NULL.
- When a field contains zero, it means the value is explicitly set to zero.
- Blank spaces or empty strings are different from NULL.
- An empty string is a string with no characters.
- It is distinct from NULL, which represents the absence of a value.
Correlated subquery: These are queries which select the data from a table referenced in the outer query. It is not considered as an independent query as it refers to another table and refers the column in a table.
Non-Correlated subquery: This query is an independent query where the output of subquery is substituted in the main query.
SELECT DISTINCT Salary FROM employee_table ORDER BY Salary DESC LIMIT 1 OFFSET 2;
In this query:
- SELECT DISTINCT Salary selects only distinct salary values to avoid duplicates.
- ORDER BY Salary DESC sorts the salaries in descending order, placing the highest salary at the top.
- LIMIT 1 OFFSET 2 limits the result to one row starting from the third row, effectively retrieving the third-highest salary.
By utilizing the ORDER BY clause in descending order and combining it with LIMIT and OFFSET, we can efficiently fetch the third-highest salary from the employee_table
- The 'WHERE' clause is used to filter rows before any grouping or aggregation is performed.
- It is applied to individual rows in the result set based on a specified condition.
- The 'WHERE' clause is used with the 'SELECT', 'UPDATE', 'DELETE', and 'INSERT' statements.
SELECT * FROM employees WHERE department = 'Sales';
HAVING Clause:
- The 'HAVING' clause is used to filter rows after the grouping has been done.
- It is applied to grouped rows based on a specified condition.
- The 'HAVING' clause is used with the 'GROUP BY' clause in SELECT statements that contain aggregate functions.
SELECT department, COUNT(*) as total_employees FROM employees GROUP BY department HAVING COUNT(*) > 5;
Key Differences:
The 'WHERE' clause is used with individual rows, while the 'HAVING' clause is used with groups of rows.
'WHERE' is applied before data is grouped, whereas 'HAVING' is applied after data is grouped.
Aggregate functions like COUNT, SUM, AVG, etc., can be used in the 'HAVING' clause but not in the 'WHERE' clause.
'WHERE' filters rows based on conditions involving individual columns, while 'HAVING' filters groups based on conditions involving aggregate values.
- Data Security: Views can restrict access to certain columns or rows of a table, providing an additional layer of security by controlling the data that users can retrieve.
- Simplifying Complex Queries: Views simplify complex queries by predefining joins, aggregations, or filters, making it easier for users to retrieve the required data without dealing with intricate SQL syntax each time.
- Data Abstraction: Views abstract the underlying data model, allowing users to interact with a simplified representation of the data, which can be particularly useful in scenarios where the database schema is complex.
- Performance Optimization: Views can enhance query performance by storing the results of a query and allowing users to access the data more efficiently, especially when dealing with frequently executed queries.
- Code Reusability: Views promote code reusability by encapsulating commonly used queries, reducing redundancy and promoting a more modular approach to database querying.
- Logical Data Independence: Views provide logical data independence by separating the way data is stored from the way it is presented, enabling changes to the underlying tables without affecting the applications that use the views.
CREATE PROCEDURE sp_GetEmployeeDetails AS BEGIN SELECT * FROM Employees; END
2- Enhanced Security: Stored Procedures provide an additional layer of security by controlling access to data tables. Users can execute the procedure without needing direct table access.
3- Code Reusability: Stored Procedures promote code reusability as the same procedure can be called from multiple applications or scripts, reducing redundancy and promoting consistency.
4- Ease of Maintenance: Changes to database schema or logic can be centralized within the Stored Procedure, making maintenance and updates more manageable.
Disadvantages of Stored Procedures:
1- Limited Portability: Stored Procedures are often database-specific, which can limit portability across different database management systems.
2- Debugging Complexity: Debugging Stored Procedures can be more challenging compared to debugging application code, as they run on the database server and may not provide detailed error messages.
3- Version Control: Managing versions of Stored Procedures can be cumbersome, especially in environments with multiple developers working on the same database.
4- Performance Overhead: In some cases, poorly optimized Stored Procedures can lead to performance issues, especially if they involve complex logic or inefficient queries.
SQL operators are symbols used to perform operations on one or more values in SQL statements. These operators include arithmetic operators (+, -, *, /), comparison operators (=, <>, >, <, >=, <=), logical operators (AND, OR, NOT), and more. They help manipulate data, filter results, and perform calculations in SQL queries. Understanding and utilizing SQL operators is essential for writing efficient and effective SQL queries. below more categorized answer.
- Arithmetic operators: These operators are used to perform mathematical operations on numerical data. The categories of this operators are addition (+), subtraction (-), multiplication (*), division (/), remainder/modulus (%), etc.
- Logical operators: These operators evaluate the expressions and return their results in True or False. This operator includes ALL, AND, ANY, ISNULL, EXISTS, BETWEEN, IN, LIKE, NOT, OR, UNIQUE.
- Comparison operators: These operators are used to perform comparisons of two values and check whether they are the same or not. It includes equal to (=), not equal to (!= or <>), less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), not less than (!<), not greater than (!>), etc.
- Bitwise operators: It is used to do bit manipulations between two expressions of integer type. It first performs conversion of integers into binary bits and then applied operators such as AND (& symbol), OR (|, ^), NOT (~), etc.
- Compound operators: These operators perform operations on a variable before setting the variable's result to the operation's result. It includes Add equals (+=), subtract equals (-=), multiply equals (*=), divide equals (/=), modulo equals (%=), etc.
- String operators: These operators are primarily used to perform concatenation and pattern matching of strings. It includes + (String concatenation), += (String concatenation assignment), % (Wildcard), [] (Character(s) matches), [^] (Character(s) not to match), _ (Wildcard match one character), etc.
Joins are used to merge two tables or retrieve data from tables. It depends on the relationship between tables. According to the ANSI standard, the following are the different types of joins used in SQL:
- INNER JOIN
- SELF JOIN
- LEFT OUTER JOIN
- RIGHT OUTER JOIN
- FULL OUTER JOIN
- CROSS JOIN
- TRUNCATE
- ROLLBACK
- COMMIT
- DELETE
- All of the mentioned
- Finding and diagnosing slow-running queries
- Capturing the series of Transact-SQL statements that lead to a problem
- Correlating performance counters to diagnose problems
- The order of occurrence determines the order of evaluation.
- NOT will be evaluated first ;AND will be evaluated second ;OR will be evaluated last.
- AND will be evaluated first ;OR will be evaluated second ;NOT will be evaluated last.
- NOT will be evaluated first ;OR will be evaluated second; AND will be evaluated last.
- Select * from weather where temperature > (Select * from weather where city ='Paris')
- Select * from weather where temperature >'Paris' temperature
- Select * from weather where temperature > (Select city from weather where city='Paris')
- Select * from weather where temperature > (Select temperature from where city='Paris')
- UNIQUE
- Primary Key
- None of the mentioned
- Foreign Key
- The SQL Alter Table deletes data from database table.
- The SQL Alter Table clause is used to delete a database table
- The SQL Alter Table clause is used to insert data database table
- The SQL Alter Table clause modifies a table definition by altering , adding , or deleting table columns and/or constraints
- INNER JOIN
- LEFT JOIN
- FULL OUTER JOIN
- RIGHT JOIN
- After the execution of 'Delete' Operation , COMMIT and ROLLBACK statements can be performed to retrieve the lost data , while 'Truncate ' do not allow it.
- After the execution of 'Delete' and 'Truncate' operation no retrieval is possible for the lost data
- After the execution of 'Delete' and 'Truncate' operation retrieval is easily possible for the lost data
- After the execution of 'Truncate' operation , COMMIT and ROLLBACK statements can be performed to retrieve the lost data while 'Delete' does not allow it