SQL Introduction and Basic Queries Practice Questions with Solutions

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.

idnameagecoursemarks
101Rahul21Python88
102Amit22Java91
103Neha20SQL95
104Priya23Python84
105Rohit21Java90

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

idnameagecoursemarks
101Rahul21Python88
102Amit22Java91
103Neha20SQL95
104Priya23Python84
105Rohit21Java90

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

namecourse
RahulPython
AmitJava
NehaSQL
PriyaPython
RohitJava

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

idnameagecoursemarks
101Rahul21Python88
102Amit22Java91
103Neha20SQL95
104Priya23Python84
105Rohit21Java90

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

idnameagecoursemarks
101Rahul21Python88
102Amit22Java91
103Neha20SQL95
104Priya23Python84
105Rohit21Java90

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

idnameagecoursemarks
101Rahul21Python88
102Amit22Java91
103Neha20SQL95
104Priya23Python84
105Rohit21Java90

SQL Solution

SELECT id,
       name

FROM students;

Sample Output

idname
101Rahul
102Amit
103Neha
104Priya
105Rohit

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

idnameagecoursemarks
101Rahul21Python88
102Amit22Java91
103Neha20SQL95
104Priya23Python84
105Rohit21Java90

SQL Solution

SELECT age,
       marks

FROM students;

Sample Output

agemarks
2188
2291
2095
2384
2190

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

idnameagecoursemarks
101Rahul21Python88
102Amit22Java91
103Neha20SQL95
104Priya23Python84
105Rohit21Java90

SQL Solution

SELECT name,
       course,
       marks,
       age,
       id

FROM students;

Sample Output

namecoursemarksageid
RahulPython8821101
AmitJava9122102
NehaSQL9520103
PriyaPython8423104
RohitJava9021105

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

idnameagecoursemarks
101Rahul21Python88
102Amit22Java91
103Neha20SQL95
104Priya23Python84
105Rohit21Java90

SQL Solution

SELECT name AS Student_Name,
       marks AS Student_Marks

FROM students;

Sample Output

Student_NameStudent_Marks
Rahul88
Amit91
Neha95
Priya84
Rohit90

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

idnameagecoursemarks
101Rahul21Python88
102Amit22Java91
103Neha20SQL95
104Priya23Python84
105Rohit21Java90

SQL Solution

SELECT s.name,
       s.course

FROM students AS s;

Sample Output

namecourse
RahulPython
AmitJava
NehaSQL
PriyaPython
RohitJava

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 SELECT statement 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.

Scroll to Top