Refining Our Selections
In this section, we'll learn things like how to limit number of results you get back and how to sort those results.
DISTINCT
If our SELECT
query has duplicate values, we can qualify it as SELECT DISTINCT
to tell SQL to only return unique values.
ORDER BY
When you want to order your results, you can use ORDER BY
at the end of your SELECT
statements.
Pro tip: You can perform a multiple sort, where your query sorts by the 1st column first. Then it sorts any duplicates by the 2nd column.
LIMIT
LIMIT
, when used in conjunction with ORDER BY
, gives you back a subset of data.
When you want to define where your limit point starts, you write LIMIT <starting_index>,<number_of_items>
.
Note: The starting index is zero-indexed.
Pro tip: Using LIMIT
with a starting index is great for things like pagination!
Pro tip: When you want to select from a certain index all the way to the end of the table, provide a gigantic number:
LIMIT 5,99999999999999999999
LIKE
LIKE
allows you to set conditions in WHERE
that search for open-ended patterns rather than strict equality.
_
is a wildcard representing exactly 1 character. %
is a wildcard representing an indefinite number of characters (including zero).
Note: LIKE
is case insensitive, so in the example above, Mc%
and mc%
both work.
Pro tip: Any search functionality usually employs LIKE
on some level.
Last updated