You are currently viewing SQL 1.66 SQL ASC Keyword

SQL 1.66 SQL ASC Keyword

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.

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

  1. SELECT * FROM employees: Selects all columns from the employees table.
  2. ORDER BY age ASC: Sorts the results by the age column in ascending order.

Output

idfirst_namelast_nameagesalarydepartment
2JaneSmith2560000.00Marketing
5LindaJohnson2875000.00Engineering
1JohnDoe3050000.00Engineering
3EmilyDavis3570000.00Engineering
4MichaelBrown4080000.00Sales

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

  1. SELECT * FROM employees: Selects all columns from the employees table.
  2. ORDER BY department ASC, salary ASC: Sorts the results first by the department column and then by the salary column within each department in ascending order.

Output

idfirst_namelast_nameagesalarydepartment
1JohnDoe3050000.00Engineering
3EmilyDavis3570000.00Engineering
5LindaJohnson2875000.00Engineering
2JaneSmith2560000.00Marketing
4MichaelBrown4080000.00Sales

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

  1. SELECT * FROM employees: Selects all columns from the employees table.
  2. ORDER BY last_name ASC: Sorts the results by the last_name column in ascending order.

Output

idfirst_namelast_nameagesalarydepartment
4MichaelBrown4080000.00Sales
3EmilyDavis3570000.00Engineering
1JohnDoe3050000.00Engineering
5LindaJohnson2875000.00Engineering
2JaneSmith2560000.00Marketing

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.

Leave a Reply