1 – SQL SELECT, SQL WHERE, and ORDER BY

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

For our instructor lead, more in depth look at learning to write a SQL SELECT, SORT BY and WHERE clauses why not check out our video course hosted on UDEMY or enjoy the free SQL course below.

SQL Select syntax

The SELECT statement is the cornerstone of retrieving data from a database. It is used to specify the columns you want to retrieve and the table(s) from which you want to retrieve data. Start by specifying the keyword SELECT, this indicates that you would like to select some data, after this specify the columns from which you would like to select data, finally you need to specify the table where your data resides. the syntax is then as follows:
				
					SELECT column1, column2
FROM table_name
				
			
The keywords used in the SQL syntax are SELECT and FROM.  These are highlighted in blue above and most text editors such as SQL server management studio will highlight the keywords. Tables can also belong to schemas that are logical grouping of tables.  If no schema is specified then the default schema will be used. You can however also specify the schema as shown in the example below:
				
					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

The WHERE clause is used to filter data based on specified conditions. It allows you to retrieve only the rows that meet certain criteria. For example, WHERE column_name > 100 retrieves rows where the value in is greater than 100. The where clause is used after the FROM clause to filter the data. To take a more details look at how to use the where clause you can find in-depth discussion and examples in our online SQL course hosted on UDEMY.
				
					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 Data

Now, 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

In this article, we have explored the fundamental concepts of SQL SELECT statements, sorting, and filtering data. By mastering these techniques, you can retrieve data from a database that meets your specific requirements. The ORDER BY WHERE clause helps you filter the data based on conditions. These skills are crucial for efficiently working with databases and extracting valuable information from them. As you become more familiar with SQL, you’ll be able to create more complex queries to meet various data retrieval needs.

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. 

Learning SQL programming with SQL server
Learn SQL Online

Free online SQL course sections