π Ordering Data
Welcome to the Selecting Data module! This foundational learning path is designed to help you master the basics of querying data, particularly focusing on how to retrieve specific information from databases effectively.
π Creating SQL Tableβ
In this tutorial, you'll learn how to interpret and use rows in a database table. Tables are essential to storing structured data, and each row in a table represents a unique item or record.
Each row of a table represents a new item.
Each column of a table represents a specific attribute of the data, such as
id,name, orusername. These columns define the type of information stored for each item in the table.
For example, consider a table named Friends. Below is how a simple table might look:
Task: Sort a table using the ORDER BY clauseβ
Lesson Overviewβ
After using ORDER BY, we specify the column by which we want to order the entries.
For example, to sort by the name column:
The first step in ordering table is the SELECT ORDER BY helps you to arrange data in readable form Here FROM specify the table we are selecting from ountries. Here the query is ordering the number by name/.
- SQL Table
- SQL Code
- Output
| name | username |
-----------|---------|
Smith | 19 |
Jones | 60 |
Wilson | 25 |
-- creating orders
SELECT * FROM patients
ORDER BY name;
| name | age |
|---|---|
| Smith | 19 |
| Jones | 60 |
| Wilson | 25 |
Example Practiceβ
To query data from a table, use the FROM clause followed by the table's name.
For example, consider a table named Friends. Below is how a simple table might look:
- SQL Table
- SQL Code
- Output
| name | gdp |
|---------|---------|
| Greece | 187.46 |
| Sweden | 474.15 |
| Iceland | 21.6 |
| Germany | 3449.05 |
SELECT *
FROM countries
ORDER BY name;
| name | gdp |
|---|---|
| Germany | 3449.05 |
| Greece | 187.46 |
| Iceland | 21.6 |
| Sweden | 474.15 |
When requesting data with SQL staments like SELECT, we say that we are making a query. From helps in select the name col from While not necessary but its a good practice to finish the sql queries with;
By following these best practices, ASC is the default sort ordering method followed.
π Arranging ORDERS BY with ASC , DESCβ
Ordering text properties like name is different when comparing to the age We can order items in asseding starting with smalest value or deceding.
- SQL Table
- SQL Code
- Output
| name | age |
|--------|-----|
| Smith | 19 |
| Jones | 60 |
| Wilson | 25 |
SELECT *
FROM patients
ORDER BY age ASC;
| name | age |
|---|---|
| Smith | 19 |
| Wilson | 25 |
| Jones | 60 |
π§Ή Selecting with DESCβ
For Text value it order by Alphabetically When arranging numerical value the item with smallest value in that coloumn comes first
- SQL Table
- SQL Code
- Output
| name | age |
|--------|-----|
| Smith | 19 |
| Jones | 60 |
| Wilson | 25 |
SELECT *
FROM patients
ORDER BY age DESC;
| name | age |
|---|---|
| Jones | 60 |
| Wilson | 25 |
| Smith | 19 |
β What You Have Learned
This module covers essential concepts related to ordering data in SQL:
-
ORDER BY Clause
Learn how to sort query results based on one or more columns using theORDER BYkeyword. -
Ascending Order (ASC)
Understand that SQL sorts in ascending order by default, which is the same as explicitly usingASC. -
Descending Order (DESC)
Use theDESCkeyword to sort results from highest to lowest or reverse alphabetical order. -
Sort by Column Values
Practice sorting by numeric or text column values to organize data meaningfully.