Lesson 2 Flashcards

ORDER BY (9 cards)

1
Q

Why do we need to order data in SQL?

Sometimes we have to ___

A

It can be really hard to see any trends or insights with a large amount of data, so sometimes we have to summarize, rearrange, or reshape data in order to learn more.

Sometimes we need to:

  • see what the range of values looks like
  • what the distribution looks like
  • helps us see our max and min
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you sort data in SQL?

A

The ORDER BY clause helps us sort data

  • Can sort in descending or ascending order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Sorting an entire dataset can have negative impacts to optimization since ____.

  • What can it do?

Caution (best practice in real world)

A

Sorting an entire dataset can have negative impacts to optimization since it can take a lot of time and power to sort a very large dataset.

  • So, it can really hurt your query performance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

By default, ORDER BY sorts in ascending / descending order

A

By default, ORDER BY sorts in ascending order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you sort by descending order?

For example, how can you pull everything from the orders table and sort by order_date, in descending order?

(Write code)

A

To sort by descending order, you can add desc after the column name:

SELECT
   *
FROM
   orders
ORDER BY
   order_date desc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

SQL clauses do / do not have to go in a certain order, or you can get an error and they won’t work.

A

SQL clauses DO have to go in a certain order, or you can get an error and they won’t work.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you pull everything from the orders table and sort by the order_date?

(Write code & pay attention to order of SQL clauses)

A
SELECT
   *
FROM
   orders
ORDER BY
   order_date
  • There are other SQL clauses that sometimes go between FROM and ORDER BY, but we’ll learn those later.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What will the following query return?

SELECT
    order_id,
    customer_id,
    order_date
FROM
    orders
ORDER BY
    customer_id, order_date desc
A
  • In SQL, you can order by multiple columns.
    • It’ll order by the first column and then by the second column, etc.

This will sort our entire dataset by:

  • customer_id, ascending
    • (because that’s the default)
  • and THEN within each customer_id, it’ll sort by order_date, descending
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In some SQL dialects (especially in newer cloud platforms like Snowflake for example), you can use a shorthand notation to represent the column name.

How can you rewrite the following code?

SELECT
    order_id,
    customer_id,
    order_date
FROM
    orders
ORDER BY
    customer_id, order_date desc

This will make you NOT look like a newb! :p

A
SELECT
    order_id,
    customer_id,
    order_date
FROM
    orders
ORDER BY
    2, 3 desc
  • We can replace the column names in the ORDER BY statement [wc?] with numbers.
  • The shorthand notation is based on the order of the columns in the SELECT statement.
    • customer_id is the 2nd column in the SELECT statement
    • order_date is the 3rd column in the SELECT statement

Makes query look cleaner - a lot of people use it in the real world

How well did you know this?
1
Not at all
2
3
4
5
Perfectly