SQL (Structured Query Language) is a powerful tool for managing and querying relational databases. One of the fundamental operations in SQL is the SELECT statement, which allows you to retrieve data from a database. In this article, we will explore how to use SQL SELECT statements to retrieve, sort and filter data. We will look at SQL SELECT, SQL WHERE, and SQL ORDER BY. Sorting allows you to arrange your results in a specific order, while filtering lets you narrow down the data you retrieve. By the end of this article, you will have a solid understanding of how to SELECT, SORT and FILTER data.
Keywords:
order by and sql, order by desc in sql, order by in sql descending, sql where, sql where like, sql query where clause, sql where between, sql where is not equal
SQL SELECT, SQL WHERE, and ORDER BY table of contents
Learn SQL online
SQL Select syntax
SELECT column1, column2
FROM table_name
SELECT column1, column2
FROM schema_name.table_name
Order by and SQL
To sort data, use the ORDER BY clause at the end of your SELECT statement. This clause sorts the result set based on one or more columns, either in ascending (ASC) or descending (DESC) order. For example, ORDER BY column_name ASC sorts data in ascending order. Specify the ASC of DESC keyword after the ORDER BY statement in order to control which direction the returned data set is sorted by. Take note of the data type being used. If you sort a character column with values 11, 22, 130 then the order would be 11,130, 22. If the data type was numeric then you would get the expected 11, 22, 130.
SELECT column1, column2
FROM schema_name.table_name
ORDER BY column1 DESC,
column2 ASC
SQL query where clause
SELECT column1, column2
FROM schema_name.table_name
WHERE column1 = 10
ORDER BY column1 DESC,
column2 ASC
SQL Where Between
A useful filter to use in the where clause is SQL where between. The SQL between clause will filter data that is inclusively between two values. You can see the SQL between syntax below:
SELECT column1, column2
FROM schema_name.table_name
WHERE column1 BETWEEN 10 AND 20
SQL Where LIKE
The SQL WHERE LIKE a filter will return records that match a particular pattern. You can use the wildcard % to match any character. The below query will filter any rows where column1 contains the word one and can have any characters on either side of it.
SELECT column1, column2
FROM schema_name.table_name
WHERE column1 LIKE '%ONE%'
SQL examples
Let’s illustrate the concepts with some examples.
Example 1: Sorting Data
Suppose we have a table called “Employees” with columns: EmployeeID, FirstName, LastName, and Salary. We want to retrieve all employees sorted by their salary in descending order:
SELECT EmployeeID, FirstName, LastName, Salary
FROM Employees
ORDER BY Salary DESC;
In this query, we select the desired columns and specify the “ORDER BY” clause to sort the results by the “Salary” column in descending order.
Example 2: Filtering DataNow, let’s say we want to retrieve employees with a salary greater than £50,000. We can use the “WHERE” clause for this:
SELECT EmployeeID, FirstName, LastName, Salary
FROM Employees
WHERE Salary > 50000;
In this query, we only retrieve rows where the “Salary” is greater than £50,000.
Conclusion
Please see the other SQL learning articles below as part of this learning SQL online course.
If you would like an instructor lead SQL course that goes into more detail with worked examples then we also provide a video course to learn SQL online hosted on UDEMY.