Basic Statements
Last updated
Last updated
The ORDER BY statement allows us to sort our results using the data in any column. If you are familiar with Excel or Google Sheets, using ORDER BY is similar to sorting a sheet using a column. A key difference, however, is that using ORDER BY in a SQL query only has temporary effects, for the results of that query, unlike sorting a sheet by column in Excel or Sheets.
In other words, when you use ORDER BY in a SQL query, your output will be sorted that way, but then the next query you run will encounter the unsorted data again. It's important to keep in mind that this is different than using common spreadsheet software, where sorting the spreadsheet by column actually alters the data in that sheet until you undo or change that sorting. This highlights the meaning and function of a SQL "query."
The ORDER BY statement always comes in a query after the SELECT and FROM statements, but before the LIMIT statement. If you are using the LIMIT statement, it will always appear last. As you learn additional commands, the order of these statements will matter more.
Remember DESC
can be added after the column in your ORDER BY statement to sort in descending order, as the default is to sort in ascending order.
Multiple columns
Here, we saw that we can ORDER BY more than one column at a time. When you provide a list of columns in an ORDER BY command, the sorting occurs using the leftmost column in your list first, then the next column from the left, and so on. We still have the ability to flip the way we order using DESC.
Using the WHERE statement, we can display subsets of tables based on conditions that must be met. You can also think of the WHERE command as filtering the data.
This video above shows how this can be used, and in the upcoming concepts, you will learn some common operators that are useful with the WHERE' statement.
Common symbols used in WHERE statements include:
>
(greater than)
<
(less than)
>=
(greater than or equal to)
<=
(less than or equal to)
=
(equal to)
!=
(not equal to)
The WHERE statement can also be used with non-numeric data. We can use the =
and !=
operators here. You need to be sure to use single quotes (just be careful if you have quotes in the original text) with the text data, not double quotes.
Commonly when we are using WHERE with non-numeric data fields, we use the LIKE, NOT, or INoperators. We will see those before the end of this lesson!
Creating a new column that is a combination of existing columns is known as a derived column (or "calculated" or "computed" column). Usually you want to give a name, or "alias," to your new column using the AS keyword.
This derived column, and its alias, are generally only temporary, existing just for the duration of your query. The next time you run a query and access this table, the new column will not be there.
If you are deriving the new column from existing columns using a mathematical expression, then these familiar mathematical operators will be useful:
*
(Multiplication)
+
(Addition)
-
(Subtraction)
/
(Division)
Consider this example:
Here we divide the standard paper dollar amount by the total order amount to find the standard paper percent for the order, and use the AS keyword to name this new column "std_percent." You can run this query on the next page if you'd like, to see the output.
The LIKE operator is extremely useful for working with text. You will use LIKE within a WHERE clause. The LIKE operator is frequently used with %
. The %
tells us that we might want any number of characters leading up to a particular set of characters or following a certain set of characters, as we saw with the google syntax above. Remember you will need to use single quotes for the text you pass to the LIKE operator, because of this lower and uppercase letters are not the same within the string. Searching for 'T' is not the same as searching for 't'. In other SQL environments (outside the classroom), you can use either single or double quotes.
The IN operator is useful for working with both numeric and text columns. This operator allows you to use an =
, but for more than one item of that particular column. We can check one, two or many column values for which we want to pull data, but all within the same query. In the upcoming concepts, you will see the OR operator that would also allow us to perform these tasks, but the IN operator is a cleaner way to write these queries.
In most SQL environments, although not in our Udacity's classroom, you can use single or double quotation marks - and you may NEED to use double quotation marks if you have an apostrophe within the text you are attempting to pull.
In our Udacity SQL workspaces, note you can include an apostrophe by putting two single quotes together. For example, Macy's in our workspace would be 'Macy''s'.
The NOT operator is an extremely useful operator for working with the previous two operators we introduced: IN and LIKE. By specifying NOT LIKE or NOT IN, we can grab all of the rows that do not meet a particular criteria.
The AND operator is used within a WHERE statement to consider more than one logical clause at a time. Each time you link a new statement with an AND, you will need to specify the column you are interested in looking at. You may link as many statements as you would like to consider at the same time. This operator works with all of the operations we have seen so far including arithmetic operators (+
, *
, -
, /
). LIKE, IN, and NOT logic can also be linked together using the AND operator.
Sometimes we can make a cleaner statement using BETWEEN than we can using AND. Particularly this is true when we are using the same column for different parts of our AND statement. In the previous video, we probably should have used BETWEEN.
Instead of writing :
we can instead write, equivalently:
Examples
Write a query that returns all the orders where the standard_qty is over 1000, the poster_qty is 0, and the gloss_qty is 0.
Using the accounts table, find all the companies whose names do not start with 'C' and end with 's'.
When you use the BETWEEN operator in SQL, do the results include the values of your endpoints, or not? Figure out the answer to this important question by writing a query that displays the order date and gloss_qty
data for all orders where gloss_qty is between 24 and 29. Then look at your output to see if the BETWEEN operator included the begin and end values or not.
You should notice that there are a number of rows in the output of this query where the gloss_qty
values are 24 or 29. So the answer to the question is that yes, the BETWEEN operator in SQL is inclusive; that is, the endpoint values are included. So the BETWEEN statement in this query is equivalent to having written "WHERE gloss_qty >= 24 AND gloss_qty <= 29."
You will notice that using BETWEEN is tricky for dates! While BETWEEN is generally inclusive of endpoints, it assumes the time is at 00:00:00 (i.e. midnight) for dates. This is the reason why we set the right-side endpoint of the period at '2017-01-01'.
Similar to the AND operator, the OR operator can combine multiple statements. Each time you link a new statement with an OR, you will need to specify the column you are interested in looking at. You may link as many statements as you would like to consider at the same time. This operator works with all of the operations we have seen so far including arithmetic operators (+
, *
, -
, /
), LIKE, IN, NOT, AND, and BETWEEN logic can all be linked together using the OR operator.
When combining multiple of these operations, we frequently might need to use parentheses to assure that logic we want to perform is being executed correctly. The video below shows an example of one of these situations. SELECT *
Statement
How to Use It
Other Details
SELECT
SELECT Col1, Col2, ...
Provide the columns you want
FROM
FROM Table
Provide the table where the columns exist
LIMIT
LIMIT 10
Limits based number of rows returned
ORDER BY
ORDER BY Col
Orders table based on the column. Used with DESC.
WHERE
WHERE Col > 5
A conditional statement to filter your results
LIKE
WHERE Col LIKE '%me%'
Only pulls rows where column has 'me' within the text
IN
WHERE Col IN ('Y', 'N')
A filter for only rows with column of 'Y' or 'N'
NOT
WHERE Col NOT IN ('Y', 'N')
NOT is frequently used with LIKE and IN
AND
WHERE Col1 > 5 AND Col2 < 3
Filter rows where two or more conditions must be true
OR
WHERE Col1 > 5 OR Col2 < 3
Filter rows where at least one condition must be true
BETWEEN
WHERE Col BETWEEN 3 AND 5