Section 8 - Refining Our Selections Flashcards
What does the DISTINCT function do?
It prints back only results that are unique/distinct.
Write a function that prints only unique author_lastname s from a table called le_book_shop.
SELECT DISTINCT author_lastname FROM le_book_shop;
Write a query that selects distinct author_firstname s and author_lastname s from a table called book_store.
SELECT DISTINCT(CONCAT(author_firstname, ‘ ‘, author_lastname)) AS ‘Distinct Author Names’ FROM book_store;
What function do you use for returning only unique results?
DISTINCT
What are some ways you could use the ORDER BY function?
- Find the bestselling product in order store
- Order movies by released date
- Order albums by best reviewed
- Order yoyos by how many yoyos we have in stock
- Order our milk by most likely to be expired to least likely to be expired.
Should there be a space in the ORDER BY function?
YES.
Don’t get tripped up by it! It’s intentional. AUTO_INCREMENT uses an underscore, but ORDER BY does not.
What does the ORDER BY function do?
It puts things in order based on what we want it to order by. (Alphabetically, numerically, calendar date, sales data, etc etc.)
Does the ORDER BY function go before or after the “FROM “ syntax?
It goes AFTER the FROM syntax.
For example:
SELECT mvp_winners FROM sports_stuff ORDER BY year;
What command would you use for changing with direction the order goes in? Eg, change from ascending to descending / alphabetical to reverse alphabetical.
You would use the DESC command.
The DESC syntax comes after(!) ORDER BY.
For Example:
SELECT bachelor_winners FROM reality_show_contestants ORDER BY year DESC;
The above would list the results from newest to oldest.
(Ascending is the default ordering, which is lowest to highest. 1935 is a “lower” year, at least numerically, than 2019 would be, so this is why Ascending order would have older years first, newer years last.)
What is the command for sorting results in ascending order?
ASC
In context, it would look like this:
…FROM books ORDER BY released_year ASC;
Can ORDER BY be used with strings/letters as well as with numbers?
A resounding YES!
Do you have to match what you’re selecting with what you’re ordering by?
NO, these can be different!
For example:
SELECT released_year FROM books ORDER BY copies_sold_since_released;
Provided those are all valid column names, this would be a perfectly valid expression/function/query/whatever you want to call it.
If we are selecting multiple columns, what’s a shortcut we can use in our ORDER BY statement so that we don’t have to write out the column name?
Give an example.
Answer:
You use a number in place of the column name. The number corresponds to the order you write the column names.
For example:
SELECT rockstar, drugs_taken, lawsuits_won FROM music_penal_records ORDER BY 2;
In the above example, 2 refers to the drugs_taken column, because it’s second in our list of 3 total columns (rockstar, drugs_taken, lawsuits_won).
Can you ORDER BY more than one column?
YES.
Is the order sorting in which you use the ORDER BY function on multiple columns important? Why?
YES, it is important.
It’s important because your results may change based on what you ORDER BY first. For example, if you sorted first by last_name, and then by first_name, the majority of your results would be sorted by first_name.
In the above example, the sort would first sort by last_name, then sort again by first_name, which would reshuffle the results to be shuffled by first_name.
What does the LIMIT function do?
It allows us to specify a number for how many results we want to SELECT.