Interview Query

Miscellaneous Functions and Clauses

Here, we’ll go over a few miscellaneous functions and clauses. First, we will go over the full execution order of SQL functions, as that will introduce two new clauses.

Full Execution Order of SQL Queries

The full execution order of SQL queries is as follows:

  1. FROM and JOIN
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. ORDER BY
  6. LIMIT

We already know what the first four do, so what about the last two?

ORDER BY

The ORDER BY clause handles ordering the query’s output (duh). Like the GROUP BY clause, we can specify multiple columns to order by, and the query will be sorted in the sequence the arguments are given.

The ORDER BY clause will sort entries in ascending order by default. You must specify the DESC keyword after a column name to make SQL sort the entries into descending order.

Like the GROUP BY clause, you can specify the column(s) to order by using numbers instead of referring to a column by name. So ORDER BY 1 DESC will order by the first column in the query. This is useful when you define a column before the ORDER BY clause, as you can’t refer to those columns by an alias.

LIMIT

The LIMIT clause simply limits the number of rows a query will return. For example, putting LIMIT 10 at the end of a query would make SQL only return the first ten rows of the query’s result.

Useful Functions

IFNULL

As we know, NULL is not 0 or "" or "Nothing" or 1/1/0 AD. But, sometimes we would like this to be the case. Luckily, the IFNULL function will do just that. The syntax for IFNULL is:

IFNULL([Input],[output to return if input is NULL])

ROUND

For data that will be displayed to non-technical stakeholders, it is often preferable not to show the full floating-point calculation of division. The ROUND function lets us round the input to a specific decimal place, which is specified in the function’s second argument.

CONCAT

Sometimes, we want to represent two text columns as a single text column. For example, we may want to show a first_name column and last_name column together as a user’s full name. CONCAT function can accomplish this. Just make sure to add a space between first_name and last_name in this case, as CONCAT will not place anything in between the arguments passed to it!

0%

Completed

You have 56 sections remaining on this learning path.

Loading pricing options..