Basics of SQL
Basics of SQL Interview with follow-up questions
1. What is SQL and why is it used?
SQL (Structured Query Language) is a domain-specific language designed for managing data in relational database management systems (RDBMS). It is the standard interface used to communicate with databases like PostgreSQL, MySQL, SQL Server, Oracle, and SQLite.
SQL is used to:
- Define structure —
CREATE,ALTER,DROPtables, indexes, views, schemas (DDL) - Manipulate data —
INSERT,UPDATE,DELETErecords (DML) - Query data —
SELECTrows from one or many tables with filtering, sorting, and aggregation - Control access —
GRANTandREVOKEpermissions (DCL) - Manage transactions —
COMMIT,ROLLBACK,SAVEPOINT(TCL)
SQL is declarative: you describe what data you want, not how to retrieve it. The database engine's query planner determines the optimal execution path.
Why it remains relevant in 2026: SQL is the lingua franca of structured data. Modern data warehouses (BigQuery, Snowflake, Redshift), analytics platforms, ORMs, and cloud databases all expose SQL or a close dialect. Knowing SQL is foundational for backend engineering, data engineering, and data analysis roles.
Common follow-up: Interviewers often ask you to distinguish SQL (the language standard) from a specific RDBMS like MySQL or PostgreSQL, or to explain when you might choose SQL over a NoSQL solution.
Follow-up 1
Can you name some SQL databases?
Some popular SQL databases include MySQL, PostgreSQL, Oracle Database, Microsoft SQL Server, and SQLite.
Follow-up 2
What are some advantages of using SQL?
There are several advantages of using SQL:
- Ease of use: SQL provides a simple and intuitive syntax for querying and manipulating data in databases.
- Scalability: SQL databases can handle large amounts of data and can scale horizontally or vertically as needed.
- Data integrity: SQL databases enforce data integrity through constraints and relationships, ensuring the accuracy and consistency of data.
- Security: SQL databases offer robust security features, including user authentication, access control, and encryption.
- ACID compliance: SQL databases support ACID (Atomicity, Consistency, Isolation, Durability) properties, ensuring reliable and transactional data operations.
Follow-up 3
How does SQL differ from NoSQL?
SQL and NoSQL are two different types of database management systems:
- Data model: SQL databases use a structured data model based on tables, rows, and columns, while NoSQL databases use various data models, such as key-value, document, columnar, or graph.
- Schema: SQL databases have a predefined schema that defines the structure of the data, while NoSQL databases are schema-less or have a flexible schema.
- Query language: SQL databases use SQL as the query language, which provides a standardized way to interact with the data. NoSQL databases may use different query languages or APIs.
- Scalability: SQL databases typically scale vertically by adding more resources to a single server, while NoSQL databases are designed for horizontal scalability, allowing data to be distributed across multiple servers.
- Use cases: SQL databases are commonly used for structured data and complex queries, while NoSQL databases are often used for unstructured or semi-structured data and high scalability requirements.
2. What are the different types of SQL?
SQL statements are grouped into five categories based on their purpose:
| Category | Full Name | Purpose | Key Commands |
|---|---|---|---|
| DDL | Data Definition Language | Define and modify schema structure | CREATE, ALTER, DROP, TRUNCATE, RENAME |
| DML | Data Manipulation Language | Read and modify data rows | SELECT, INSERT, UPDATE, DELETE, MERGE |
| DCL | Data Control Language | Manage permissions and access | GRANT, REVOKE |
| TCL | Transaction Control Language | Manage transaction boundaries | COMMIT, ROLLBACK, SAVEPOINT, SET TRANSACTION |
| DQL | Data Query Language | Strictly data retrieval (some classify SELECT here separately) |
SELECT |
Key distinctions interviewers probe:
DROPvsTRUNCATE— both remove data, butDROPremoves the table definition too;TRUNCATEkeeps the structure and is non-logged (faster, but not always transactional).DELETEis DML (logged, can be rolled back);TRUNCATEis DDL in most databases (minimal logging).MERGE(also calledUPSERT) is DML that combines INSERT and UPDATE in one statement, supported in SQL Server, Oracle, and PostgreSQL (asINSERT ... ON CONFLICT).
In practice, most interviews focus on DDL and DML. TCL becomes important when discussing transaction management and ACID properties.
Follow-up 1
Can you explain the difference between DDL, DML, and DCL?
DDL (Data Definition Language) is used to define and manage the structure of the database. It includes statements like CREATE, ALTER, and DROP. DML (Data Manipulation Language) is used to manipulate the data within the database. It includes statements like SELECT, INSERT, UPDATE, and DELETE. DCL (Data Control Language) is used to control access to the database. It includes statements like GRANT and REVOKE.
Follow-up 2
What is the role of TCL in SQL?
TCL (Transaction Control Language) is used to manage transactions within the database. It includes statements like COMMIT, ROLLBACK, and SAVEPOINT. Transactions are used to ensure the integrity and consistency of the data.
Follow-up 3
What is the purpose of DQL in SQL?
DQL (Data Query Language) is used to retrieve data from the database. It includes statements like SELECT, which allows you to specify the columns and conditions to filter the data you want to retrieve.
3. What is a relational database?
A relational database organizes data into tables (also called relations), where each table consists of rows (records/tuples) and columns (attributes/fields). Tables are linked to one another through keys — primary keys uniquely identify rows within a table, while foreign keys reference primary keys in other tables to establish relationships.
Core characteristics:
- Data is stored in a structured, tabular format with a defined schema
- Relationships between tables are explicit and enforced through referential integrity constraints
- SQL is the standard language for querying and manipulating data
- The relational model was formalized by Edgar F. Codd in 1970
How tables relate: A customers table might have a primary key customer_id, and an orders table would have a foreign key customer_id referencing it. This avoids duplicating customer data in every order row.
Common examples of RDBMS: PostgreSQL, MySQL, SQL Server, Oracle Database, SQLite.
Interview follow-up: Be ready to contrast a relational database with a NoSQL database — interviewers frequently ask when you would choose one over the other.
Follow-up 1
What are the advantages of a relational database?
Relational databases offer several advantages:
- Structure: The tabular structure of a relational database allows for easy organization and management of data.
- Data Integrity: Relational databases enforce data integrity through constraints and relationships, ensuring accuracy and consistency.
- Flexibility: Relational databases can handle complex queries and provide powerful tools for data analysis and reporting.
- Scalability: Relational databases can scale vertically (adding more resources to a single server) and horizontally (distributing data across multiple servers).
- Security: Relational databases offer robust security features to protect data from unauthorized access.
Follow-up 2
Can you give an example of a relational database?
One example of a relational database is MySQL. MySQL is an open-source relational database management system that is widely used for web applications and other data-driven projects. It provides a comprehensive set of features for managing and querying relational data.
Follow-up 3
How does a relational database differ from a non-relational database?
Relational databases and non-relational databases (also known as NoSQL databases) differ in their data models and storage structures.
Relational databases:
- Organize data into tables with rows and columns.
- Use a structured query language (SQL) for querying and manipulating data.
- Establish relationships between tables through keys.
- Enforce data integrity through constraints.
Non-relational databases:
- Use various data models, such as key-value, document, columnar, or graph.
- Do not rely on a fixed schema and can handle unstructured or semi-structured data.
- Provide flexible scalability and high performance for specific use cases.
- Do not enforce strict data integrity constraints like relational databases.
The choice between a relational database and a non-relational database depends on the specific requirements of the application and the nature of the data being stored.
4. What is a SQL query?
A SQL query is a statement written in SQL that requests an action from the database — most commonly retrieving data, but also inserting, updating, or deleting it.
The most fundamental query is the SELECT statement:
SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1;
Query execution order (not the same as the written order — interviewers test this):
FROM/JOIN— identify the source tablesWHERE— filter rowsGROUP BY— group remaining rowsHAVING— filter groupsSELECT— compute output columnsDISTINCT— remove duplicatesORDER BY— sort resultsLIMIT/OFFSET— restrict row count
Understanding this logical order explains why you cannot reference a SELECT alias in a WHERE clause (the alias doesn't exist yet at that point), but you can reference it in ORDER BY.
Types of queries: DML queries (SELECT, INSERT, UPDATE, DELETE), DDL queries (CREATE TABLE, ALTER TABLE), and queries using subqueries, CTEs (WITH), or window functions for more complex data retrieval.
Follow-up 1
Can you write a basic SQL query?
Sure! Here's an example of a basic SQL query to retrieve all records from a table named 'customers':
SELECT * FROM customers;
Follow-up 2
What are the different parts of a SQL query?
A SQL query consists of several parts:
- SELECT: Specifies the columns to be retrieved from the database.
- FROM: Specifies the table(s) from which to retrieve the data.
- WHERE: Specifies the conditions that the data must meet.
- GROUP BY: Groups the data based on specified columns.
- HAVING: Specifies conditions for the grouped data.
- ORDER BY: Specifies the order in which the data should be sorted.
- LIMIT: Specifies the maximum number of rows to be returned.
Follow-up 3
What is the difference between a query and a subquery?
A query is a standalone statement that retrieves data from a database. It can be used to perform various operations such as retrieving, inserting, updating, or deleting data.
On the other hand, a subquery is a query that is nested within another query. It is used to retrieve data that is based on the result of another query. The result of a subquery is used as a condition or value in the outer query. Subqueries can be used in SELECT, INSERT, UPDATE, or DELETE statements.
5. What is a SQL table?
A SQL table is the fundamental storage unit in a relational database. It is a two-dimensional structure organized into columns (which define the data type and name of each attribute) and rows (which hold individual records).
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
department_id INT,
hire_date DATE,
salary DECIMAL(10,2)
);
Key properties:
- Each column has a defined data type (INT, VARCHAR, DATE, etc.) that enforces what values it can hold
- Each row represents one entity instance (one employee, one order, etc.)
- A primary key uniquely identifies each row — no two rows can share the same primary key value
- Tables can reference other tables via foreign keys, creating relationships
Interview nuance: Tables store persistent data on disk. Views are virtual tables derived from queries. Temporary tables (CREATE TEMP TABLE) exist only for the session. CTEs (Common Table Expressions) are in-memory named result sets scoped to a single query.
Follow-up 1
How is data organized in a SQL table?
Data in a SQL table is organized in rows and columns. Each row represents a single record, and each column represents a specific attribute or field of the record. The intersection of a row and a column is called a cell, which holds the actual data value. The table schema defines the structure of the table, including the names and data types of the columns.
Follow-up 2
What are the different types of keys in a SQL table?
There are several types of keys that can be used in a SQL table:
Primary Key: A primary key is a unique identifier for each record in a table. It ensures that each record is uniquely identified and provides a way to enforce data integrity.
Foreign Key: A foreign key is a field in a table that refers to the primary key of another table. It establishes a relationship between two tables and enforces referential integrity.
Unique Key: A unique key is a constraint that ensures the values in a column or a set of columns are unique. Unlike a primary key, a unique key can contain null values.
Composite Key: A composite key is a key that consists of multiple columns. It is used when a single column cannot uniquely identify a record, but the combination of multiple columns can.
Candidate Key: A candidate key is a column or a set of columns that can uniquely identify a record. It is a potential primary key candidate.
Follow-up 3
How do you create a table in SQL?
To create a table in SQL, you can use the CREATE TABLE statement. Here is an example:
CREATE TABLE table_name (
column1 datatype1,
column2 datatype2,
...
);
table_nameis the name of the table you want to create.column1,column2, etc. are the names of the columns in the table.datatype1,datatype2, etc. are the data types of the columns.
You can also specify additional constraints, such as primary keys, foreign keys, and unique keys, using the CONSTRAINT keyword. For example:
CREATE TABLE table_name (
column1 datatype1 CONSTRAINT pk_column1 PRIMARY KEY,
column2 datatype2 CONSTRAINT fk_column2 REFERENCES other_table(other_column),
column3 datatype3 CONSTRAINT uk_column3 UNIQUE,
...
);
Live mock interview
Mock interview: Basics of SQL
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.