Introduction to SQL and the ASC Keyword
SQL (Structured Query Language) is a standard language for accessing and manipulating databases. One of the fundamental tasks in SQL is to sort data, which can be achieved using the ORDER BY
clause. The ASC
keyword specifies that the sorting should be in ascending order.
In this tutorial, we’ll explore the use of the ASC
keyword with various examples. By the end, you’ll have a solid understanding of how to sort data in ascending order using SQL.
![](https://dumudigitikakenya.com/wp-content/uploads/2024/05/SQL-ASC-Keyword-1024x576.png)
Table of Contents
Creating a Sample Database and Table
First, let’s create a sample database and table to work with. We’ll use a table called employees
with the following structure:
CREATE DATABASE company;
USE company;
CREATE TABLE employees (
id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
age INT,
salary DECIMAL(10, 2),
department VARCHAR(50)
);
INSERT INTO employees (id, first_name, last_name, age, salary, department) VALUES
(1, 'John', 'Doe', 30, 50000.00, 'Engineering'),
(2, 'Jane', 'Smith', 25, 60000.00, 'Marketing'),
(3, 'Emily', 'Davis', 35, 70000.00, 'Engineering'),
(4, 'Michael', 'Brown', 40, 80000.00, 'Sales'),
(5, 'Linda', 'Johnson', 28, 75000.00, 'Engineering');
Basic Syntax of ORDER BY with ASC
The ORDER BY
clause is used to sort the result set of a query by one or more columns. The ASC
keyword specifies that the sorting should be in ascending order. By default, ORDER BY
sorts in ascending order even if ASC
is not explicitly specified.
Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 ASC, column2 ASC, ...;
Example 1: Sorting by a Single Column
Let’s sort the employees by their age
in ascending order.
SELECT * FROM employees
ORDER BY age ASC;
Explanation
- SELECT * FROM employees: Selects all columns from the
employees
table. - ORDER BY age ASC: Sorts the results by the
age
column in ascending order.
Output
id | first_name | last_name | age | salary | department |
---|---|---|---|---|---|
2 | Jane | Smith | 25 | 60000.00 | Marketing |
5 | Linda | Johnson | 28 | 75000.00 | Engineering |
1 | John | Doe | 30 | 50000.00 | Engineering |
3 | Emily | Davis | 35 | 70000.00 | Engineering |
4 | Michael | Brown | 40 | 80000.00 | Sales |
Example 2: Sorting by Multiple Columns
Now, let’s sort the employees first by department
and then by salary
within each department in ascending order.
SELECT * FROM employees
ORDER BY department ASC, salary ASC;
Explanation
- SELECT * FROM employees: Selects all columns from the
employees
table. - ORDER BY department ASC, salary ASC: Sorts the results first by the
department
column and then by thesalary
column within each department in ascending order.
Output
id | first_name | last_name | age | salary | department |
---|---|---|---|---|---|
1 | John | Doe | 30 | 50000.00 | Engineering |
3 | Emily | Davis | 35 | 70000.00 | Engineering |
5 | Linda | Johnson | 28 | 75000.00 | Engineering |
2 | Jane | Smith | 25 | 60000.00 | Marketing |
4 | Michael | Brown | 40 | 80000.00 | Sales |
Example 3: Sorting by a Column in a Specific Order
Let’s sort the employees by last_name
in ascending order.
SELECT * FROM employees
ORDER BY last_name ASC;
Explanation
- SELECT * FROM employees: Selects all columns from the
employees
table. - ORDER BY last_name ASC: Sorts the results by the
last_name
column in ascending order.
Output
id | first_name | last_name | age | salary | department |
---|---|---|---|---|---|
4 | Michael | Brown | 40 | 80000.00 | Sales |
3 | Emily | Davis | 35 | 70000.00 | Engineering |
1 | John | Doe | 30 | 50000.00 | Engineering |
5 | Linda | Johnson | 28 | 75000.00 | Engineering |
2 | Jane | Smith | 25 | 60000.00 | Marketing |
Conclusion
In this tutorial, we learned how to use the ASC
keyword with the ORDER BY
clause to sort data in ascending order. We explored different scenarios including sorting by a single column, multiple columns, and specific columns.
Understanding how to sort data is a fundamental skill in SQL, and mastering the use of ORDER BY
with ASC
will help you efficiently organize and retrieve data from your databases.