An_Introduction_to_SQL_1731971471
An_Introduction_to_SQL_1731971471
Alkistis Pourtsidou
Queen Mary, University of London
✦ https://www.indeed.co.uk/
WHY DO WE NEED DATABASES?
✦ Rapid queries
✦ EachSince
database contains
a single servermany tables many databases, each
can support
containing many tables, with each table having a variety
✦ Each table contains many columns
of columns, it’s easy to get lost when you’re working with
databases.
✦ But keeping These
things commands
under control is will help figure out what’s
straightforward!
available:
I SHOW DATABASES;
I SHOW TABLES IN database;
I SHOW COLUMNS IN table;
I DESCRIBE table; - shows the columns and their
types
duction to
SQL Variable Types
VARIABLE TYPES
✦ SQLSQL
supports a variety
supports a veryoflarge
different
numberformats for storing
of di↵erent information
formats for
internal storage of information.
Numeric
I INTEGER, SMALLINT, BIGINT
I NUMERIC(w,d), DECIMAL(w,d) - numbers with width
w and d decimal places
I REAL, DOUBLE PRECISION - machine and database
dependent
I FLOAT(p) - floating point number with p binary
digits of precision
VARIABLE TYPES
uction to
SQL Variable Types (cont’d)
✦ SQL supports a variety of different formats for storing information
Character
I CHARACTER(L) - a fixed-length character of length L
I CHARACTER VARYING(L) or VARCHAR(L) - supports
maximum length of L
Binary
I BIT(L), BIT VARYING(L) - like corresponding
characters
I BINARY LARGE OBJECT(L) or BLOB(L)
Temporal
I DATE
I TIME
I TIMESTAMP
SQL: THE BASICS
✦ Enough intro! Let’s dive into SQL with hands-on examples
✦ Let’s first define a function that takes our query, stored as a string, as an input.
✦ Then shows the result as a formatted data frame (we’ll see this in action in a
bit…)
SQL: THE BASICS
SQL: THE BASICS
SQL: THE BASICS
SQL: THE BASICS
SQL: THE BASICS
✦ DESC: Descending
SQL: THE BASICS
✦ We will use SELECT to return the station column from the stations table using the
table.column syntax, i.e. stations.station in our case
✦ We also return the COUNT of the number of rows from the trips table
✦ To tell the database how the stations and trips tables are connected, we’ll use JOIN and
ON.
✦ JOIN specifies which tables should be connected
✦ INNER JOIN means rows will only be returned where there is a much in the columns
specified by ON
✦ Tables are connected ON trips.start_station = stations.id
✦ Then we group by the station column so that COUNT will give the number of trips for
each station separately
✦ Finally we ORDER BY descending order
SQL: THE BASICS
SQL: THE BASICS
SQL: THE BASICS
✦ Let’s slightly expand this query to see which are the most
popular round-trip stations:
EXERCISES/TASKS
✦ How many trips lasted more than half an hour? (this induces
extra charges)
✦ www.postgresql.org
✦ It has two tables: one for bank account complaints and one
for credit card complaints
PostgreSQL
✦ First let’s see how the credit card complaints table looks like.
PostgreSQL
✦ Then let’s get the number of records using the COUNT
function
✦ Let’s see how many records in each table have null values for
the consumer complaint narrative field
✦ Syntax is simple:
PostgreSQL: Views
✦ Let’s have a look:
PostgreSQL: String Concatenation
✦ For example say we have a “month” field and a “year” field but
we need to show “month-year” instead
✦ Syntax:
✦ https://www.w3schools.com/sql/
✦ http://www.sql-tutorial.net/
✦ https://www.kaggle.com/learn/sql
✦ https://www.dataquest.io/blog/sql-basics/
✦ https://www.dataquest.io/blog/sql-intermediate/
✦ https://www.dataquest.io/blog/python-pandas-databases/