SQL (Structured Query Language) is the standard language used to communicate with relational databases. It allows developers, data analysts, and database administrators to create databases, store data, retrieve records, update information, and delete unwanted data efficiently.
Almost every modern application uses SQL behind the scenes. Whether you are using an e-commerce website, banking application, hospital management system, or social media platform, SQL is responsible for managing the data.
If you want to become a Data Analyst, Data Scientist, Backend Developer, Software Engineer, Database Administrator (DBA), or Business Intelligence Developer, learning SQL is one of the most important skills. SQL Introduction and Basic Queries practice questions with solutions help to understand the concepts.
What is SQL?
SQL (Structured Query Language) is a programming language specifically designed for managing and manipulating data stored in relational database management systems (RDBMS).
SQL allows users to:
- Create databases
- Create tables
- Insert records
- Retrieve records
- Update data
- Delete records
- Manage database security
- Control transactions
SQL is supported by almost every popular relational database, including:
- MySQL
- PostgreSQL
- Microsoft SQL Server
- Oracle Database
- SQLite
- MariaDB
Why Learn SQL?
SQL is one of the most in-demand technical skills because data is at the core of every business.
Benefits of Learning SQL
- Easy to learn
- High industry demand
- Essential for Data Analytics
- Used in Backend Development
- Required for Business Intelligence
- Works with almost every database
- Powerful data filtering and reporting capabilities
Applications of SQL
SQL is widely used in:
- Banking Systems
- Hospital Management Systems
- Student Management Systems
- E-commerce Websites
- Railway Reservation Systems
- Employee Management Systems
- Inventory Management
- CRM Software
- ERP Applications
- Social Media Platforms
Features of SQL
- Simple syntax
- Platform independent
- Supports relational databases
- Fast data retrieval
- Powerful filtering
- Supports transactions
- Supports joins
- Highly secure
- Standardized language
- Easy integration with programming languages
Types of SQL Commands
SQL commands are divided into five categories.
1. DDL (Data Definition Language)
DDL commands define the database structure.
Examples:
CREATE
ALTER
DROP
TRUNCATE
RENAME
2. DML (Data Manipulation Language)
DML commands manipulate data.
Examples:
INSERT
UPDATE
DELETE
3. DQL (Data Query Language)
Used for retrieving data.
Example:
SELECT
4. DCL (Data Control Language)
Used for permissions.
Examples:
GRANT
REVOKE
5. TCL (Transaction Control Language)
Used to control transactions.
Examples:
COMMIT
ROLLBACK
SAVEPOINT
Basic SQL Syntax
SELECT column_name
FROM table_name
WHERE condition;
Example:
SELECT *
FROM students;
SQL Rules
- SQL keywords are not case-sensitive.
- Every SQL statement ends with a semicolon (
;) (recommended). - Table names should be meaningful.
- Use uppercase for SQL keywords to improve readability.
Example:
SELECT *
FROM employees;
Sample Database Used in This Chapter
Throughout this chapter, we will use the following students table.
| id | name | age | course | marks |
|---|---|---|---|---|
| 101 | Rahul | 21 | Python | 88 |
| 102 | Amit | 22 | Java | 91 |
| 103 | Neha | 20 | SQL | 95 |
| 104 | Priya | 23 | Python | 84 |
| 105 | Rohit | 21 | Java | 90 |
1. SQL Query to Display All Records
Problem Statement
Write an SQL query to display all records from the students table.
SQL Solution
SELECT *
FROM students;
Sample Output
| id | name | age | course | marks |
|---|---|---|---|---|
| 101 | Rahul | 21 | Python | 88 |
| 102 | Amit | 22 | Java | 91 |
| 103 | Neha | 20 | SQL | 95 |
| 104 | Priya | 23 | Python | 84 |
| 105 | Rohit | 21 | Java | 90 |
Explanation
The SELECT * statement retrieves every column from the specified table.
* means all columns.
This is commonly used while checking the contents of a table.
Concepts Covered
- SELECT
- Table
- Retrieve Records
2. SQL Query to Display Only Student Names
Problem Statement
Write an SQL query to display only the name column.
SQL Solution
SELECT name
FROM students;
Sample Output
| name |
|---|
| Rahul |
| Amit |
| Neha |
| Priya |
| Rohit |
Explanation
Instead of retrieving every column, SQL allows you to select only the required columns.
This improves performance because only necessary data is returned.
Concepts Covered
- SELECT
- Specific Columns
- Data Retrieval
3. SQL Query to Display Student Name and Course
Problem Statement
Write an SQL query to display only the student name and course.
SQL Solution
SELECT name,
course
FROM students;
Sample Output
| name | course |
|---|---|
| Rahul | Python |
| Amit | Java |
| Neha | SQL |
| Priya | Python |
| Rohit | Java |
Explanation
SQL allows multiple columns to be selected by separating them with commas.
Only the selected columns are returned in the result.
Concepts Covered
- Multiple Columns
- SELECT Statement
- SQL Basics
4. SQL Query to Display Student Marks
Problem Statement
Write an SQL query to display only the marks of all students.
Sample Table
| id | name | age | course | marks |
|---|---|---|---|---|
| 101 | Rahul | 21 | Python | 88 |
| 102 | Amit | 22 | Java | 91 |
| 103 | Neha | 20 | SQL | 95 |
| 104 | Priya | 23 | Python | 84 |
| 105 | Rohit | 21 | Java | 90 |
SQL Solution
SELECT marks
FROM students;
Sample Output
| marks |
|---|
| 88 |
| 91 |
| 95 |
| 84 |
| 90 |
Explanation
The SELECT statement can retrieve any specific column from a table.
Here, only the marks column is displayed.
This approach reduces unnecessary data retrieval and improves query performance.
Concepts Covered
- SELECT Statement
- Single Column Selection
- Data Retrieval
5. SQL Query to Display All Courses
Problem Statement
Write an SQL query to display the course column from the students table.
Sample Table
| id | name | age | course | marks |
|---|---|---|---|---|
| 101 | Rahul | 21 | Python | 88 |
| 102 | Amit | 22 | Java | 91 |
| 103 | Neha | 20 | SQL | 95 |
| 104 | Priya | 23 | Python | 84 |
| 105 | Rohit | 21 | Java | 90 |
SQL Solution
SELECT course
FROM students;
Sample Output
| course |
|---|
| Python |
| Java |
| SQL |
| Python |
| Java |
Explanation
This query returns all values stored in the course column.
Since no filtering is applied, every record is displayed.
Notice that duplicate values (such as Python and Java) are also shown because SELECT does not remove duplicates by default.
Concepts Covered
- SELECT
- Column Selection
- Duplicate Values
6. SQL Query to Display Student ID and Name
Problem Statement
Write an SQL query to display only the Student ID and Student Name from the students table.
Sample Table
| id | name | age | course | marks |
|---|---|---|---|---|
| 101 | Rahul | 21 | Python | 88 |
| 102 | Amit | 22 | Java | 91 |
| 103 | Neha | 20 | SQL | 95 |
| 104 | Priya | 23 | Python | 84 |
| 105 | Rohit | 21 | Java | 90 |
SQL Solution
SELECT id,
name
FROM students;
Sample Output
| id | name |
|---|---|
| 101 | Rahul |
| 102 | Amit |
| 103 | Neha |
| 104 | Priya |
| 105 | Rohit |
Explanation
This query retrieves only the id and name columns.
Selecting only the required columns reduces the amount of data transferred from the database.
Concepts Covered
- SELECT Statement
- Multiple Column Selection
- SQL Basics
7. SQL Query to Display Student Age and Marks
Problem Statement
Write an SQL query to display the age and marks of all students.
Sample Table
| id | name | age | course | marks |
|---|---|---|---|---|
| 101 | Rahul | 21 | Python | 88 |
| 102 | Amit | 22 | Java | 91 |
| 103 | Neha | 20 | SQL | 95 |
| 104 | Priya | 23 | Python | 84 |
| 105 | Rohit | 21 | Java | 90 |
SQL Solution
SELECT age,
marks
FROM students;
Sample Output
| age | marks |
|---|---|
| 21 | 88 |
| 22 | 91 |
| 20 | 95 |
| 23 | 84 |
| 21 | 90 |
Explanation
The SELECT statement allows you to retrieve any combination of columns from a table.
The order of columns in the result depends on the order specified in the query.
Concepts Covered
- SELECT
- Multiple Columns
- Data Retrieval
8. SQL Query to Display All Columns in a Different Order
Problem Statement
Write an SQL query to display all columns in the following order:
- Name
- Course
- Marks
- Age
- ID
Sample Table
| id | name | age | course | marks |
|---|---|---|---|---|
| 101 | Rahul | 21 | Python | 88 |
| 102 | Amit | 22 | Java | 91 |
| 103 | Neha | 20 | SQL | 95 |
| 104 | Priya | 23 | Python | 84 |
| 105 | Rohit | 21 | Java | 90 |
SQL Solution
SELECT name,
course,
marks,
age,
id
FROM students;
Sample Output
| name | course | marks | age | id |
|---|---|---|---|---|
| Rahul | Python | 88 | 21 | 101 |
| Amit | Java | 91 | 22 | 102 |
| Neha | SQL | 95 | 20 | 103 |
| Priya | Python | 84 | 23 | 104 |
| Rohit | Java | 90 | 21 | 105 |
Explanation
SQL returns the columns exactly in the order specified in the SELECT statement.
This feature is useful when generating reports or exporting data.
Concepts Covered
- Column Ordering
- SELECT Statement
- SQL Reports
9. SQL Query Using Column Aliases
Problem Statement
Write an SQL query to display student names and marks using meaningful column aliases.
Sample Table
| id | name | age | course | marks |
|---|---|---|---|---|
| 101 | Rahul | 21 | Python | 88 |
| 102 | Amit | 22 | Java | 91 |
| 103 | Neha | 20 | SQL | 95 |
| 104 | Priya | 23 | Python | 84 |
| 105 | Rohit | 21 | Java | 90 |
SQL Solution
SELECT name AS Student_Name,
marks AS Student_Marks
FROM students;
Sample Output
| Student_Name | Student_Marks |
|---|---|
| Rahul | 88 |
| Amit | 91 |
| Neha | 95 |
| Priya | 84 |
| Rohit | 90 |
Explanation
A column alias gives a temporary name to a column in the query result.
The original table structure remains unchanged.
Aliases improve readability, especially in reports and dashboards.
Concepts Covered
- AS Keyword
- Column Alias
- SQL Formatting
10. SQL Query Using Table Aliases
Problem Statement
Write an SQL query using a table alias to display student names and courses.
Sample Table
| id | name | age | course | marks |
|---|---|---|---|---|
| 101 | Rahul | 21 | Python | 88 |
| 102 | Amit | 22 | Java | 91 |
| 103 | Neha | 20 | SQL | 95 |
| 104 | Priya | 23 | Python | 84 |
| 105 | Rohit | 21 | Java | 90 |
SQL Solution
SELECT s.name,
s.course
FROM students AS s;
Sample Output
| name | course |
|---|---|
| Rahul | Python |
| Amit | Java |
| Neha | SQL |
| Priya | Python |
| Rohit | Java |
Explanation
A table alias assigns a temporary name to a table.
Instead of repeatedly writing:
students.name
you can simply write:
s.name
Table aliases become extremely useful when working with:
- Joins
- Subqueries
- Large SQL queries
- Multiple tables
Concepts Covered
- Table Alias
- AS Keyword
- SQL Readability
- Query Simplification
11. SQL Query to Display a Constant Value
Problem Statement
Write an SQL query to display a constant text value.
SQL Solution
SELECT 'Welcome to SQL Learning' AS Message;
Sample Output
| Message |
|---|
| Welcome to SQL Learning |
Explanation
SQL can return constant values without referencing any table.
This is useful for:
- Testing SQL queries
- Displaying messages
- Learning SQL syntax
Concepts Covered
- SELECT Statement
- Constant Values
- AS Keyword
12. SQL Query Using a Mathematical Expression
Problem Statement
Write an SQL query to calculate the sum of two numbers.
SQL Solution
SELECT 100 + 250 AS Total;
Sample Output
| Total |
|---|
| 350 |
Explanation
SQL supports arithmetic operations such as:
- Addition (
+) - Subtraction (
-) - Multiplication (
*) - Division (
/) - Modulus (
%) (supported in many databases)
These expressions can be used directly in SQL queries or with table columns.
Concepts Covered
- Arithmetic Operators
- SQL Expressions
- Calculations
13. SQL Query to Display the Current Date
Problem Statement
Write an SQL query to display the current system date.
SQL Solution (MySQL)
SELECT CURDATE() AS Current_Date;
Sample Output
| Current_Date |
|---|
| 2026-08-08 |
Note: The displayed date depends on the database server’s current date.
Explanation
CURDATE() returns the current date in YYYY-MM-DD format.
It is commonly used in:
- Attendance Systems
- Payroll Software
- Order Management
- Reporting Applications
Concepts Covered
- Date Functions
- CURDATE()
- SQL Functions
14. SQL Query to Display the Current Database
Problem Statement
Write an SQL query to display the name of the currently selected database.
SQL Solution (MySQL)
SELECT DATABASE() AS Current_Database;
Sample Output
| Current_Database |
|---|
| studentdb |
Note: The database name depends on the database currently selected in your SQL session.
Explanation
DATABASE() returns the name of the database currently in use.
This function is useful when working with multiple databases to verify the active database.
Concepts Covered
- DATABASE()
- Database Functions
- SQL Environment
15. SQL Query to Display the Database Version
Problem Statement
Write an SQL query to display the version of the database server.
SQL Solution (MySQL)
SELECT VERSION() AS Database_Version;
Sample Output
| Database_Version |
|---|
| 8.0.xx |
Note: The exact version number depends on the installed MySQL server.
Explanation
VERSION() returns the version of the database server.
Developers often check the version to ensure compatibility with SQL features and application requirements.
Concepts Covered
- VERSION()
- Database Information
- SQL Functions
Chapter Summary
In this chapter, you learned the fundamentals of SQL (Structured Query Language), the standard language used to interact with relational databases. SQL enables developers, database administrators, and data analysts to store, retrieve, update, and manage data efficiently.
You explored the basic structure of SQL queries, the different categories of SQL commands, and how to retrieve data using the SELECT statement. You also learned how to select specific columns, use aliases, perform simple calculations, and retrieve useful database information such as the current date, active database, and database version.
The concepts covered in this chapter form the foundation for all advanced SQL topics.
Throughout this chapter, you covered:
- Introduction to SQL
- Features of SQL
- Applications of SQL
- Types of SQL Commands
- DDL
- DML
- DQL
- DCL
- TCL
- SQL Syntax
- SELECT Statement
- Selecting Specific Columns
- Column Ordering
- Column Aliases
- Table Aliases
- SQL Expressions
- Date Functions
- Database Functions
- Version Functions
These basics will help you write efficient SQL queries and prepare you for filtering, sorting, grouping, and joining data in upcoming chapters.
Key Takeaways
- SQL stands for Structured Query Language.
- SQL is used to manage relational databases.
- The
SELECTstatement retrieves data from tables. - Specific columns can be selected instead of retrieving all columns.
- Column aliases improve the readability of query results.
- Table aliases simplify long SQL queries.
- SQL supports arithmetic expressions and built-in functions.
CURDATE()returns the current date in MySQL.DATABASE()returns the currently selected database.VERSION()displays the database server version.
Frequently Asked Questions (FAQs)
1. What is SQL?
SQL (Structured Query Language) is the standard language used to create, retrieve, update, and manage data in relational databases.
2. What are the five types of SQL commands?
SQL commands are categorized into:
- DDL (Data Definition Language)
- DML (Data Manipulation Language)
- DQL (Data Query Language)
- DCL (Data Control Language)
- TCL (Transaction Control Language)
3. What is the purpose of the SELECT statement?
The SELECT statement is used to retrieve data from one or more tables.
Example:
SELECT *
FROM students;
4. What is the difference between SELECT * and selecting specific columns?
SELECT *
FROM students;
Retrieves all columns.
SELECT name,
marks
FROM students;
Retrieves only the specified columns, making the query faster and easier to read.
5. What is a column alias?
A column alias provides a temporary name for a column in the query result.
Example:
SELECT name AS Student_Name
FROM students;
Aliases improve report readability without changing the original table structure.
6. What is a table alias?
A table alias gives a temporary name to a table.
Example:
SELECT s.name
FROM students AS s;
Table aliases are especially useful when working with joins and complex queries.
7. Can SQL perform calculations?
Yes. SQL supports arithmetic expressions.
Example:
SELECT 25 * 4 AS Result;
Output:
100
8. Where is SQL used in real-world applications?
SQL is widely used in:
- Banking Systems
- Hospital Management Systems
- Student Management Systems
- Employee Management Systems
- Inventory Management
- E-commerce Websites
- CRM Applications
- ERP Software
- Business Intelligence Tools
- Data Analytics
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
