Statements


Base


From

I like to start with FROM because it is where we always start. We are basically wanting to process/analyze a specific set of data and that always starts with FROM. It is used to specify which table to select from that specific database from a certain project.

SELECT     column1, col2,....fields..
FROM   table_name

Select

Is usually the first command in your query, as you see above (or a long reference to a select statement). It is used to specify:

  • the column/field names of the table you want to select
  • both FROM and SELECT will get more complicated as we start nesting aggregations and conditions into them, but remember the base of all these statements

Select Distinct

Is a specific SELECT where we only choose the unique value in column(s)

  • In a table, a column often contains many duplicate values, and sometimes we only want the list of unique (distinct) values in that column(s)
SELECT DISTINCT     Country 
FROM                CountryTable;

Where

Is used to filter the chosen records based on a specified condition(s)

  • you can concatenate multiple conditions using logical operators AND, OR
  • narrows your query so that the database returns only the data with an exact value match or the data that matches a certain condition that you want to satisfy
  • WHERE is also used in UPDATE, DELETE and many other SQL commands
SELECT DISTINCT     Country 
FROM                CountryTable
WHERE               Country = "blah"
                    AND Country = "blue"
                    OR  Country = "blee";

That covers the basics of a query, now we move on and build statements with more common commands that help, sort, filter, aggregate, calculate, summarize….and many other functions I haven’t used yet.

Arrange


Command Syntax (MySQL/DB2) Description Example (MySQL/DB2)
ORDER BY SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC; ORDER BY keyword is used to sort the result-set in ascending or descending order. The default is ascending. In case of multiple columns in ORDER BY, the sorting will be done in the sequence of the appearance of the arguments. SELECT f_name, l_name, dep_id FROM employees ORDER BY dep_id DESC, l_name;
This displays the first name, last name, and department ID of employees, first sorted in descending order of department IDs and then sorted alphabetically as per their last names.

Order By Condition

  • just as it sounds, it sorts/orders ASC by default, always good to specify anyways
  • DESC has to be specified
  • Because it sorts in ASC order by default, it will sort alphabetically if used on character strings
SELECT     *
FROM       TableName
ORDER BY   condition 1 ASC, 
           condition 2 DESC;
# or ----
ORDER BY   condition 1 DESC, condition 2 DESC;
ORDER BY   condition 1, condition 2;     -- if you're too lazy to type DESC
several columns
  • just list them one after the other (as shown above) with their specific type of order you want ASC or DESC

Order By Column

  • We can also order by columns instead of conditions
  • Let’s say we use SELECT to extract 2 columns: title & pages
  • We know that the default sorting order is ascending
  • If we want to sort the results by number of pages in ascending order we use
SELECT  title, pages
FROM    book
ORDER BY pages;

# ___ OR WE CAN USE 2 THE SECOND COLUMN ON THE LIST OF SELECTED COLUMNS __
ORDER BY 2;

Assign


Aliases

  • Aliases are used to give a table or a column a temporary name
  • They make column names, calculations or aggregations more descriptive and readable
  • Only exist for the duration of that specific query they occupy
  • AS is used to create an alias, and in most languages you can skip it all together
SELECT  column_name  AS alias_name
FROM    table_name;

# ---- OR -----
SELECT  column_name(s)
FROM    table_name AS  alias_name;

Comparison Operators


>, >=, <, <=, =, !=, OR, AND

We should be familiar with these operators, there is no need to go on.