Welcome to SQL Task 4 of my Elevate Labs internship! This project demonstrates intermediate SQL querying techniques, focusing on sorting, limiting data, and implementing pagination.
The primary goal of this task is to:
- Sort Data: Use
ORDER BYto retrieve records in ascending and descending order. - Multi-Level Sorting: Apply sorting across multiple columns simultaneously.
- Limit Results: Restrict the number of rows returned using
LIMIT. - Conditional Sorting: Combine filtering (
WHERE) with sorting (ORDER BY). - Pagination: Navigate through subsets of data using
OFFSET.
To successfully run the scripts in this project, ensure you have the following:
- SQL Server: MySQL, MariaDB, or any compatible SQL engine.
- SQL Client: A command-line interface (CLI) or a graphical tool like MySQL Workbench.
The project uses a students table to demonstrate data querying techniques.
| Column Name | Data Type | Description |
|---|---|---|
🔑 id |
INT | Primary Key, Auto-increment |
👤 name |
VARCHAR(50) | Full name of the student |
🔢 age |
INT | Student's age |
📧 email |
VARCHAR(100) | Student's email address |
🏢 department |
VARCHAR(50) | Department of the student |
Follow these steps to initialize and test the database:
- Start Server: Ensure your SQL server instance is running.
- Connect: Use your preferred SQL client to connect to the server.
- Execute Script: Open and run the
task4.sqlfile.source path/to/task4.sql;
- Verify: Check the output to observe the results of different SQL query operations.
The task4.sql script includes several essential SQL operations:
SELECT * FROM students ORDER BY age ASC;
SELECT * FROM students ORDER BY age DESC;SELECT * FROM students ORDER BY department ASC, age DESC;SELECT * FROM students ORDER BY age DESC LIMIT 3;SELECT * FROM students WHERE department = 'CSE' ORDER BY age ASC;SELECT * FROM students ORDER BY id LIMIT 2 OFFSET 0;
SELECT * FROM students ORDER BY id LIMIT 2 OFFSET 2;Tip
You can execute the different SELECT queries individually to observe how ORDER BY, LIMIT, and OFFSET sort, restrict, and paginate the result set differently.