SELECT
, LIMIT
, aliasing columns with AS
LIMIT
clauseSELECT
-ing one or more columnsAS
employees
database (e.g.\c employees
)SELECT *
FROM employees
;
LIMIT
clauseUnlike the SELECT
clause and the FROM
clause, the LIMIT
clause is an optional part of a SQL statement that limits the number of rows (n) returned.
SELECT *
FROM employees
LIMIT 10
;
Each SQL clause and its function:
SELECT
: Selects the columns to return.FROM
: Points to the table the SQL is querying from.LIMIT
: (optional) Limits the number of rows returned (in the case above, n = 10)Worth noting:
SELECT
, FROM
, and LIMIT
all appear in the same SQL statement, the order must be written in this order!Further to note:
FROM
-> SELECT
-> LIMIT
, which means that despite only printing out a subset of rows, a SQL query with LIMIT
clause still uses up the same amount of memory as the same SQL query without the LIMIT clause.
LIMIT
returns a random sample of n rows, so the output will not always be the same every time. Therefore, LIMIT
should only be used as a way to preview data.LIMIT
, TOP
is used. For more information, read this Wikipedia article.Exercises
On your own, please do the following:
Write a SQL query that returns all columns from the employees
table, but only 100 rows.
Write a SQL query that returns all columns from the departments
table, but only 100 rows. Why does the output not add up to 100 rows?
What’s wrong with the SQL statement below? Please debug until it runs correctly.
SELECT *
FROM employees
LIMIT 1,000
;
SELECT
-ing one or more columnsTo return all columns from a table, exactly in the order they are stored:
SELECT *
FROM table_name
;
To return a single column from a table:
SELECT column_name
FROM table_name
;
To return multiple columns from a table, use commas to separate the columns:
SELECT
column_name,
column_name,
column_name,
...
column_name
FROM table_name
;
Worth noting:
SELECT
clause.Exercises
On your own:
Write the SQL query that returns only the column for storing first_name
in the employees
table.
Write the SQL query that returns two columns from the employees
table: first_name
and last_name
.
Further add on to the query in question 2 by limiting the output to 20 rows.
AS
We can give existing columns new names using another SQL reserved keyword AS
. The syntax is as follows:
SELECT
column_name AS new_column_name
FROM table_name
;
Worth noting:
AS
is omitted, the aliasing will still work. However, this is not proper SQL syntax and should be avoided.Exercises
On your own:
departments
. However, rename the column dept_no
to department_number
and rename the column dept_name
to department_name
.There is a somewhat trivial use case for creating new columns:
SELECT
1,
'one'
FROM departments
;
The number of rows returned equals to how many rows there are originally in the departments
table.
We can verify this by selecting more columns from the same table and also renaming the newly created columns:
SELECT
1 AS my_first_numeric_column,
'one' AS my_first_text_column,
dept_no,
dept_name
FROM departments
;
From the trivial example above, we learn many things:
When a newly created column is unnamed, PostgreSQL by default displays it as ?column?
.
When we want something evaluated as a number, we enter it without quotation marks (e.g. 1).
When we want something evaluated as text, we enter it with single quotation marks (e.g. ‘one’).
For more naming conventions, see the SQL style guide for more details.
Some common debugging methods:
Make sure that entities (e.g. table names and column names) are spelled correctly! SQL will try its best to be helpful in its error messages to correct misspellings.
Make sure that SQL reserve words (e.g. SELECT
, FROM
, LIMIT
) are spelled correctly! If you are using a text editor like Atom or Sublime, only correctly spelled SQL reserve words will change color when editing a file saved as a .sql
file.
Make sure that SQL statements ending in semicolon ;
or else the statement will not run.
Make sure that the order of the clauses are correct.
Are you missing a comma somewhere? This is common when referring to multiple columns in the SELECT
statement. Having cleanly styled SQL will help make the debugging (and comma chasing) easier.
In your breakout rooms, please do the following exercises together:
Write the SQL query that generates exactly the following output using the employees
table:
Debug the following SQL statement so that it returns 3 columns including the first name, last name, and employee number from the employees table:
SELECT
first_name
last_name
employee_no
FROM employee
LIMIT 10
The output should look like this:
How many columns will the following SQL query return? Why?
SELECT *,
dept_name
FROM departments
;
Why is the following SQL statement syntactically correct, but not returning the expected 2 columns?
SELECT
first_name
last_name
FROM employees
LIMIT 10
;