SQL Interview Questions and Answers for internship
-
What is SQL?
- Answer: SQL (Structured Query Language) is a domain-specific language used for managing and manipulating data held in a relational database management system (RDBMS). It allows users to perform tasks such as retrieving, inserting, updating, and deleting data.
-
What are the different types of SQL statements?
- Answer: SQL statements can be broadly categorized into DDL (Data Definition Language) for defining database structure (CREATE, ALTER, DROP), DML (Data Manipulation Language) for manipulating data (SELECT, INSERT, UPDATE, DELETE), DCL (Data Control Language) for controlling access to data (GRANT, REVOKE), and TCL (Transaction Control Language) for managing transactions (COMMIT, ROLLBACK).
-
Explain the difference between WHERE and HAVING clauses.
- Answer: `WHERE` clause filters rows *before* grouping, while `HAVING` clause filters rows *after* grouping (typically used with aggregate functions like COUNT, SUM, AVG). `WHERE` cannot use aggregate functions.
-
What is a JOIN in SQL? Explain different types of JOINs.
- Answer: A JOIN combines rows from two or more tables based on a related column between them. Types include INNER JOIN (returns only matching rows), LEFT (OUTER) JOIN (returns all rows from the left table and matching rows from the right), RIGHT (OUTER) JOIN (returns all rows from the right table and matching rows from the left), and FULL (OUTER) JOIN (returns all rows from both tables).
-
What is a subquery? Give an example.
- Answer: A subquery is a query nested inside another query. Example: `SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);` This finds employees with salaries above the average salary.
-
What is the difference between UNION and UNION ALL?
- Answer: Both combine the result sets of two or more `SELECT` statements. `UNION` removes duplicate rows, while `UNION ALL` includes all rows, even duplicates, resulting in a faster execution.
-
Explain the use of aliases in SQL.
- Answer: Aliases provide shorter, more descriptive names for tables or columns, making queries easier to read and understand. They are created using the `AS` keyword (e.g., `SELECT order_id AS orderNumber FROM orders`).
-
What are aggregate functions in SQL? List some examples.
- Answer: Aggregate functions perform calculations on a set of values and return a single value. Examples include `COUNT()`, `SUM()`, `AVG()`, `MAX()`, `MIN()`, `MEDIAN()` (in some databases).
-
How do you handle NULL values in SQL?
- Answer: `NULL` represents the absence of a value. Use `IS NULL` or `IS NOT NULL` in the `WHERE` clause to filter for NULL values. Functions like `COALESCE()` or `ISNULL()` can replace NULLs with a specified value.
-
What is an index in SQL? Why are they used?
- Answer: An index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data. They are used to speed up `SELECT` queries, especially on large tables, by allowing the database to quickly locate specific rows without scanning the entire table.
-
What is normalization in SQL?
- Answer: Normalization is a database design technique that organizes data to reduce redundancy and improve data integrity. It involves dividing larger tables into smaller tables and defining relationships between them.
-
Explain different normal forms (1NF, 2NF, 3NF).
- Answer: 1NF eliminates repeating groups of data within a table. 2NF is built upon 1NF and eliminates redundant data that depends on only part of the primary key. 3NF eliminates redundant data that depends on non-key attributes.
-
What is a transaction in SQL?
- Answer: A transaction is a sequence of operations performed as a single logical unit of work. It ensures data integrity by either completing all operations successfully or rolling back any changes if an error occurs.
-
What are ACID properties of transactions?
- Answer: ACID stands for Atomicity (all-or-nothing), Consistency (maintains data integrity), Isolation (transactions are independent), and Durability (changes are permanent).
-
What is a view in SQL?
- Answer: A view is a virtual table based on the result-set of an SQL statement. It does not contain data itself, but provides a customized view of the underlying data.
-
What is a stored procedure?
- Answer: A stored procedure is a pre-compiled SQL code that can be stored and reused. It improves performance and helps in managing database security.
-
What are different data types in SQL?
- Answer: Common data types include INT, VARCHAR, CHAR, DATE, TIME, BOOLEAN, FLOAT, DECIMAL, etc. The specific types available may vary depending on the database system.
-
Explain different constraints in SQL.
- Answer: Constraints are rules enforced on data within a table to ensure data integrity. Examples include PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK.
-
What is a self-join?
- Answer: A self-join is a join where a table is joined with itself. It is useful when you need to compare rows within the same table.
Thank you for reading our blog post on 'SQL Interview Questions and Answers for internship'.We hope you found it informative and useful.Stay tuned for more insightful content!