# Top 15 SQL Interview Questions and Answers (with Examples)

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/blog/top-sql-interview-questions
Source observed: 2026-11-05T00:47:31.196Z
Last checked: 2026-11-05T00:47:31.196+00:00
Indexed: 2026-11-06T16:45:35.236+00:00

---

# Top 15 SQL Interview Questions and Answers (with Examples)

We are Aced (formerly Exponent), a learning platform for technology interview preparation. This Software Engineering article is an SQL interview study guide covering relational databases, SQL concepts, definitions, query syntax, database design, and common interview questions.

For system-design preparation, use our [system design questions](https://www.aced.io/blog/system-design-interview-guide?ref=exponent-blog.ghost.io) guide and [System Design Interview Course](https://www.aced.io/courses/system-design-interviews?src=blog-top-callout&ref=exponent-blog.ghost.io). We also provide sample material for [Design Instagram](https://www.aced.io/questions?src=blog&ref=exponent-blog.ghost.io), [TinyURL](https://www.aced.io/courses/system-design-interviews/url-shortener?src=blog&ref=exponent-blog.ghost.io), and [Design YouTube](https://www.aced.io/courses/system-design-interviews/design-youtube?src=blog&ref=exponent-blog.ghost.io).

## SQL Fundamentals

Software companies collect large amounts of data for analytics and product decisions. Relational databases are a common way to store it. SQL—Structured Query Language, sometimes pronounced “sequel”—is used to create, manipulate, access, search, and perform logical operations on data.

Common database technologies and languages include [MySQL](https://www.mysql.com/?ref=exponent-blog.ghost.io), [OracleSQL](https://docs.oracle.com/cd/B19306_01/server.102/b14200/intro.htm?ref=exponent-blog.ghost.io), [NoSQL](https://en.wikipedia.org/wiki/NoSQL?ref=exponent-blog.ghost.io), and [PostgreSQL](https://www.postgresql.org/?ref=exponent-blog.ghost.io). SQL has existed for more than forty years and remains widely used because it can be maintained and adapted with new features. It is relevant to data scientists, machine-learning engineers, data analysts, data engineers, and database administrators.

### Why Is SQL Important?

- **Working with large datasets:** Hardware limits may prevent reading an entire dataset. SQL lets you query only the subset you need, making analysis faster and more practical.
- **Data analysis:** SQL helps analysts manipulate large amounts of data and extract relevant information.
- **Data science:** Prototypes may use CSV or Excel files, but production use cases generally need relational databases managed with SQL.
- **Transferable knowledge:** Oracle SQL, T-SQL, NoSQL, and PostgreSQL build additional features on a common SQL foundation.

## What Is an RDBMS?

An RDBMS, or Relational Database Management System, stores data in tables made of rows and columns. A column is described here as a feature and a row as a record. Records can be manipulated with relational operators such as `=`, `>=`, and `<>`. Examples named in the article include SQL, Microsoft SQL, Oracle SQL, and MySQL.

## What Is Normalization?

Normalization removes redundant data and improves data integrity. It removes duplicate rows and organizes data by using relationships to break large tables into smaller ones. Its two purposes are reducing unnecessary duplicated data and ensuring logical storage.

## What Is a View?

A view is a virtual table built by selecting fields from one or more database tables. It can expose all rows or filter rows according to user-defined criteria. Views can provide a security boundary because users query the view rather than the underlying table and receive only the data made available when the view was created.

Example syntax:

```sql
CREATE VIEW view_name AS SELECT column_lists FROM table_name WHERE condition;
```

## What Is a Primary Key?

A primary key is one column or a combination of columns that uniquely identifies each row. A combination is a composite key. Primary-key requirements described here are:

- Every value must be **unique**.
- Values cannot be `NULL`.
- A table has **one primary key**.

For example, an insurance company’s unique customer ID can serve as a primary key.

```sql
/* Create a table Student with ID as primary key */
/*Specifying the PRIMARY KEY while creating the table*/
CREATE TABLE Students(
ID INT NOT NULL
Name VARCHAR(255)
PRIMARY KEY (ID)
);

/* Alter table add ID as primary key*/
/*Specifying the PRIMARY KEY when the table is already created*/
ALTER TABLE Students   
ADD PRIMARY KEY (ID);
```

## What Are Constraints?

Constraints specify rules for values entering a table and help maintain data consistency. They may be defined when creating a table or later with `ALTER TABLE`. If an operation violates a constraint, the operation is aborted.

- **`NOT NULL`:** Prevents null values in a column.
- **`UNIQUE`:** Requires distinct values in a column.
- **`CHECK`:** Requires values to satisfy a specified condition.
- **`DEFAULT`:** Supplies a value when none is provided.
- **`PRIMARY KEY`:** Uniquely identifies each row; it combines `NOT NULL` and `UNIQUE`.
- **`FOREIGN KEY`:** Links tables by having a child-table field point to a parent table’s primary key.
- **`INDEX`:** Creates a pointer to stored data to speed access.

## What Is a Unique Key?

A unique key is a column or group of columns whose entries are distinct, uniquely identifying records. Unlike a primary key, it may contain one `NULL` value. A table can have multiple unique keys but only one primary key.

```sql
/* Create a table Student with ID as unique key */
/* Specify UNIQUE key at the time of table creation*/
CREATE TABLE Students(
ID INT NOT NULL UNIQUE
Name VARCHAR(255)
);

/* Alter table add ID as unique key*/
/* Specify UNIQUE key when the tabe is already created*/
ALTER TABLE Students   
ADD UNIQUE (ID);
```

## What Are the Different Subsets of SQL?

- **Data Definition Language (DDL):** Creates or modifies database-object structure. Commands listed are `CREATE`, `DROP`, `ALTER`, `TRUNCATE`, `COMMENT`, and `RENAME`.
- **Data Query Language (DQL):** Selects and queries data, including with `SELECT`, and lets users specify return conditions.
- **Data Manipulation Language (DML):** Inserts, updates, deletes, or locks data with commands such as `INSERT`, `UPDATE`, `DELETE`, and `LOCK`.
- **Data Control Language (DCL):** Manages access rights and permissions with `GRANT` and `REVOKE`; the article describes these controls as primarily available to database administrators.

For more detail, see this [SQL subset reference](https://www.geeksforgeeks.org/sql-ddl-dql-dml-dcl-tcl-commands/?ref=exponent-blog.ghost.io).

## What Is a Self-Join?

A self-join joins a table to itself using a relationship between columns. The table needs multiple aliases.

```sql
SELECT a.column_name, b.column_name...
FROM table_name a, table_name b
WHERE a.common_field = b.common_field;
```

## What Are UNION, MINUS, and INTERSECT?

These operators combine or compare results from two or more `SELECT` statements.

**`UNION`** combines results:

```sql
SELECT name FROM Students
UNION
SELECT name FROM Subjects;
```

**`MINUS`** returns values in the first result that are not in the second and removes common entries:

```sql
SELECT name FROM Students   
MINUS     
SELECT name FROM Subjects;
```

**`INTERSECT`** returns common entries:

```sql
SELECT name FROM Students   
INTERSECT    
SELECT name FROM Subjects;
```

## Types of Relationships in SQL

A relational database contains tables connected by relationships:

- **One-to-one:** One row in the first table corresponds to one row in the second, and vice versa.
- **One-to-many:** One row in the first table corresponds to one or more rows in the second.
- **Many-to-one:** Multiple rows in one table correspond to a single row in another.
- **Many-to-many:** Multiple rows in one table correspond to multiple rows in another.

![One-to-one relationship](https://i.imgur.com/4h867Rw.png)

![One-to-many relationship](https://i.imgur.com/zcpZOlp.png)

![Many-to-one relationship](https://i.imgur.com/vc9N0bV.png)

![Many-to-many relationship](https://i.imgur.com/Ch17vFC.png)

## What Is a `UNIQUE` Constraint?

A `UNIQUE` constraint prevents duplicate values in a column. Like a primary key, it enforces uniqueness, but it can contain one `NULL` value.

```sql
/* Create a table Student with ID as unique column */
/* Specifying UNIQUE constraint at the time of table creation*/
CREATE TABLE Students(
ID INT UNIQUE
Name VARCHAR(255)
);

/* Alter table add ID as unique column*/
/* Specifying UNIQUE constraint when the table is already created*
ALTER TABLE Students   
ADD UNIQUE (ID);
```

## What Is a Cursor and How Do You Use It?

A cursor points to one row within a multirow result, allowing row-by-row retrieval and manipulation, often with `SELECT` when logic must be applied to each row.

Declare it:

```sql
DECLARE cursor_name CURSOR  
FOR select_statement;
```

Open it:

```sql
OPEN cursor_name;
```

Fetch each row and advance:

```sql
FETCH NEXT FROM cursor INTO list_of_variable;
```

Close it:

```sql
CLOSE cursor_name;
```

Release its resources:

```sql
DEALLOCATE cursor_name;
```

## What Are Joins in SQL?

Joins combine tables through related columns.

**`INNER JOIN`** returns matching entries:

```sql
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
```

![Inner join](https://i.imgur.com/z1sBRKI.png)

**`LEFT JOIN`** returns every left-table row and matching right-table rows:

```sql
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
```

![Left join](https://i.imgur.com/obAyHAU.png)

**`RIGHT JOIN`** returns every right-table row and matching left-table rows:

```sql
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
```

![Right join](https://i.imgur.com/8YHDZYt.png)

**`FULL JOIN`** returns rows where either side matches, along with remaining rows from both tables:

```sql
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
```

![Full join](https://i.imgur.com/1xKdPHB.png)

## What Is an Index?

An index is a data structure for quickly looking up table data. Like a book index, it avoids scanning the entire table and can improve access speed.

```sql
CREATE INDEX index_name
ON Table_Name (Column_Name);
```

The article describes these index types:

- **Unique index:** Prevents duplicate index values.
- **Clustered index:** The index order corresponds to the physical arrangement of table records.
- **Non-clustered index:** Stored separately and does not define the table’s physical order; a table can have multiple non-clustered indexes.
- **Column store index:** Stores and retrieves table data through a column-oriented index.
- **Filtered index:** Useful when a column has few distinct values.
- **Hash index:** Uses buckets and pointers to rows.

For additional detail, see this [SQL indexes guide](https://www.besanttechnologies.com/sql-indexes?ref=exponent-blog.ghost.io).

## What Is the ACID Property in a Database?

A transaction is a logical unit of data operations that keeps the database consistent before and after processing. ACID means:

- **Atomicity:** Every action succeeds or the entire transaction fails, leaving the database unchanged. `COMMIT`, `ROLLBACK`, and `AUTO-COMMIT` may be used.
- **Consistency:** The database changes state only after a successful commit and is protected against crashes.
- **Isolation:** Statements remain mutually transparent, supporting concurrency management.
- **Durability:** Once committed, a transaction persists despite system failure, power loss, or crashes.

## What Is OLTP?

Online Transaction Processing (OLTP) systems execute transaction-focused tasks and let many people perform real-time operations on databases over the internet. They prioritize concurrency and decentralization, are highly available, and often support millions of transactions per day. OLTP systems use relational databases at their core. Read more about OLTP and its benefits in this [IBM overview](https://www.ibm.com/cloud/learn/oltp?ref=exponent-blog.ghost.io).

## Conclusion

SQL is an essential database skill for real-world applications and is also used in data science and machine learning for [data preprocessing](https://en.wikipedia.org/wiki/Data_pre-processing?ref=exponent-blog.ghost.io) and [feature engineering](https://towardsdatascience.com/what-is-feature-engineering-importance-tools-and-techniques-for-machine-learning-2080b0269f10?ref=exponent-blog.ghost.io). Review these concepts and query examples to prepare for SQL interviews. [Aced](https://www.aced.io/?ref=exponent-blog.ghost.io) helps people prepare for technology interviews in product management and engineering roles.

**Credit:** [Gourav Singh Bais](https://www.linkedin.com/in/gourav-singh-bais/?ref=exponent-blog.ghost.io), an Applied Machine Learning Engineer experienced in machine-learning and deep-learning pipelines, retraining systems, and turning data-science prototypes into production-grade solutions.

## Further Aced Resources

- [Browse software engineer interview questions](https://www.aced.io/questions?role=swe&src=blog).
- [Book time with a Software Engineer coach](https://www.aced.io/coaching?role=swe) for mock interviews, career coaching, or resume review; [browse coaches](https://www.aced.io/coaching?role=swe&src=blog).
- We report a **96% rating from 5,400 reviews**.
- [Create your free account](https://www.aced.io/signup?src=blog).

### Related Courses

- [Software Engineering Interview Prep Course](https://www.aced.io/courses/software-engineering) — 5 courses, 41,700 students. It covers mock interviews, frameworks, senior-candidate advice, data structures, algorithms, system design, people management, behavioral interviews, and more for roles at Google, Amazon, Microsoft, Meta, Apple, and other companies.
- [Amazon Software Development Engineer (SDE) Interview Course](https://www.aced.io/courses/amazon-sde-interview) — 5 courses, 4,600 students. It covers data structures, algorithms, system design, detailed questions, mock interviews, and Amazon leadership principles.

### Related Blog Posts

- [Top React Interview Questions for Experienced and Beginner Developers](https://www.aced.io/blog/top-react-interview-questions) — 13 min read.
- [MongoDB Interview Questions and Answers For Experienced and Beginner Developers](https://www.aced.io/blog/mongodb-interview-questions-answers) — 12 min read.
- [The 20 Kubernetes Interview Questions & Concepts You Need to Master](https://www.aced.io/blog/top-kubernetes-interview-questions) — 11 min read.
- [Answers To The Most Common REST API Interview Questions](https://www.aced.io/blog/top-rest-api-interview-questions) — 13 min read.

![Aced Team](https://s3-us-west-2.amazonaws.com/pmlesson/2019/11/exponent-logo-fb.jpg)

![Article graphic](https://storage.ghost.io/c/a5/f5/a5f5c2f0-4c62-43c7-891b-662a0b6d68b3/content/images/2022/08/abstract-track-statistic.gif)

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fblog%2Ftop-sql-interview-questions&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fblog%2Ftop-sql-interview-questions). Opening a form does not execute a tool.