SQL Exercises for Practice
Practice SQL questions to enhance our skills in database querying and manipulation. Each question covers a different aspect of SQL, providing a comprehensive learning experience.
We have covered a wide range of topics in the sections beginner, intermediate and advanced.
- SQL Practice Exercises for Beginners
- Basic Retrieval
- Filtering
- Arithmetic Operations and Comparisons:
- Formatting
- Aggregation Functions
- SQL Practice Exercises for Intermediate
- Group By and Having
- Joins
- Window Functions
- Conditional Statements
- DateTime Operations
- SQL Practice Exercises for Advanced
- Creating and Aliasing
- Subqueries
- Indexing
- Constraints
- Views
- Stored Procedures:
- Transactions
let’s create the table schemas and insert some sample data into them.
Create Sales table
-- Create Sales table
CREATE TABLE Sales (
sale_id INT PRIMARY KEY,
product_id INT,
quantity_sold INT,
sale_date DATE,
total_price DECIMAL(10, 2)
);
-- Insert sample data into Sales table
INSERT INTO Sales (sale_id, product_id, quantity_sold, sale_date, total_price) VALUES
(1, 101, 5, '2024-01-01', 150.00),
(2, 102, 3, '2024-01-02', 90.00),
(3, 103, 2, '2024-01-02', 60.00),
(4, 104, 4, '2024-01-03', 120.00),
(5, 105, 6, '2024-01-03', 180.00);
Output:
Create Products table
-- Create Products table
CREATE TABLE Products (
product_id INT PRIMARY KEY,
product_name VARCHAR(100),
category VARCHAR(50),
unit_price DECIMAL(10, 2)
);
-- Insert sample data into Products table
INSERT INTO Products (product_id, product_name, category, unit_price) VALUES
(101, 'Laptop', 'Electronics', 500.00),
(102, 'Smartphone', 'Electronics', 300.00),
(103, 'Headphones', 'Electronics', 30.00),
(104, 'Keyboard', 'Electronics', 20.00),
(105, 'Mouse', 'Electronics', 15.00);
Output:
SQL Exercises
SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. Whether we are beginners or experienced professionals, practicing SQL exercises is essential for our skills and language mastery.
In this article, we’ll cover a series of SQL practice exercises covering a wide range of topics suitable for beginners, intermediate, and advanced learners. These exercises are designed to provide hands-on experience with common SQL tasks, from basic retrieval and filtering to more advanced concepts like joins window functions, and stored procedures.
List of SQL Exercises
- SQL Questions for Practice
- SQL Practice Exercises for Beginners
- SQL Practice Exercises for Intermediate
- SQL Practice Exercises for Advanced
- More Questions For Practice