Window Functions
Continuing with the discussion on Window Functions, you want to find the earliest customers for ZoomZoom. In a more technical term, this means you want to rank every customer according to the date they became a customer, with the earliest customer being ranked 1, the second-earliest customer being ranked 2, and so on. You can get all the customers using the following query:
SELECT customer_id, first_name, last_name, date_added FROM customers ORDER BY date_added;
The result is:
You can order the customers from the earliest to the most recent, copy the output to an Excel spreadsheet, and assign a row number to each row so that you have the rank for each customer. But this is not automatic and is prone to errors. SQL provides several ways using which you can achieve it. Later in this chapter, you will learn how to assign numbers to ordered records by using...