From the course: SQL for Exploratory Data Analysis Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Ordering and counting

Ordering and counting - PostgreSQL Tutorial

From the course: SQL for Exploratory Data Analysis Essential Training

Start my 1-month free trial

Ordering and counting

- [Instructor] It is often useful to order data from smallest value to largest value, or from the largest value down to the smallest. This is a simple thing to do in SQL. We use the order by clause and a select statement. Let's look at an example. First, we create a select statement with the value we want to order by. Let's use the age of employees. We can issue the following command, SELECT age FROM employees. If we want to order them, we can add the ORDER BY clause. In this case, SELECT age FROM employees ORDER BY age. This will sort the list by age with the smallest value returned first. If you'd like to have the largest value returned first, you can indicate this with the DESC or descending keyword. Here's an example. SELECT age FROM employees ORDER BY age DESC. Now if you'd like to sort by age and when two employees have the same age, you can add a start date column to the order by clause like this. SELECT age FROM employees ORDER BY age, start_date. You'd probably want to…

Contents