SQL Basics: Core Concepts and Syntax – SQL Interview Questions and Answers

Q1: What is SQL, and what are its primary functions?
Answer:
SQL (Structured Query Language) is a domain-specific language used to manage and manipulate relational databases. It is used to perform tasks such as querying data, updating records, inserting data, and deleting records. SQL is essential for interacting with databases, and its primary functions include:

  • Data Querying (SELECT)

  • Data Insertion (INSERT)

  • Data Updating (UPDATE)

  • Data Deletion (DELETE)

Q2: What is a relational database?
Answer:
A relational database is a database structured to recognize relationships between stored data items. It uses tables (relations) to store data in rows (records) and columns (attributes). Tables can be related to each other via foreign keys, which represent relationships between the data in different tables.

Q3: What is the difference between WHERE and HAVING clauses in SQL?
Answer:

  • WHERE: Filters rows before any grouping (used in SELECT, UPDATE, DELETE queries).

  • HAVING: Filters groups after the GROUP BY operation, often used with aggregate functions (e.g., COUNT(), SUM()) to filter aggregated results.

Q4: What is a primary key, and why is it important?
Answer:
A primary key is a column or a set of columns in a database table that uniquely identifies each row. Primary keys must contain unique values and cannot contain NULL. They ensure the integrity of data by enforcing uniqueness and serving as a reference point for relationships with other tables.

Q5: What is the difference between INNER JOIN and LEFT JOIN?
Answer:

  • INNER JOIN: Returns only the rows that have matching values in both tables.

  • LEFT JOIN: Returns all rows from the left table, along with matching rows from the right table. If there’s no match, NULL values are returned for columns from the right table.

Q6: What is a subquery, and how does it work?
Answer:
A subquery is a query nested inside another query. Subqueries can be used in SELECT, INSERT, UPDATE, and DELETE statements. They are useful for performing operations that require multiple steps, such as filtering based on aggregated data or finding records based on the result of another query.

Q7: What is normalization, and why is it important in SQL databases?
Answer:
Normalization is the process of organizing data in a database to reduce redundancy and dependency. The goal is to minimize the chances of data anomalies. It involves splitting large tables into smaller ones and using relationships (foreign keys) between them. Normal forms (1NF, 2NF, 3NF, etc.) are used to guide this process.

Q8: What are the different types of constraints in SQL?
Answer:
SQL constraints are rules applied to columns in a table to enforce data integrity. Types include:

  • NOT NULL: Ensures that a column cannot contain NULL values.

  • UNIQUE: Ensures all values in a column are unique.

  • PRIMARY KEY: Uniquely identifies a row in a table.

  • FOREIGN KEY: Ensures referential integrity between tables.

  • CHECK: Ensures that values in a column meet a specific condition.

Q9: What is a VIEW, and how does it differ from a TABLE?
Answer:
A VIEW is a virtual table that is based on the result of a query. It does not store data itself but displays data from one or more tables. Views are used to simplify complex queries, provide a layer of security, or present data in a specific format. Unlike tables, views are not directly used to store data.

Q10: What is indexing in SQL, and why is it important?
Answer:
Indexing in SQL is a technique used to speed up the retrieval of rows from a database table. Indexes create a data structure that improves the speed of search queries. However, while they improve read operations, indexes can slow down INSERT, UPDATE, and DELETE operations because of the overhead of maintaining them.