SQL
is a programming language that talks with the databasesql
statement myst end with a semi-colon ;
sql
keywords like SELECT
, FROM
is a convention, recommended but not required.*
means allWHERE
let’s you specify conditions to filter data. You use conditional operators to build your conditionsAND
and OR
let you specify multiple conditions. AND
means all conditions must be met, OR
means any condition can be metORDER BY
is for sorting data in ASC
(default) or DESC
orderWHERE
clause, or it’ll mess with the entire table or columns.SELECT
, FROM
, WHERE
, AND
, OR
, ORDER BY
=
, >
, <
, <=
, >=
, <>
We retrieve data using SELECT
1# SELECT ___ FROM ___;
2# SELECT columnName FROM tableName;
3
4# select the `title` column from the `movies` table
5SELECT title FROM movies;
6
7# select data from multiple columns
8SELECT id, title, genre, duration FROM movies;
9
10# select all columns
11SELECT * FROM movies;
WHERE
clause (within a SELECT
statement)1# SELECT ___ FROM ___ WHERE ___;
2# SELECT columnName FROM tableName WHERE condition;
3
4SELECT title FROM movies WHERE id = 2; # numbers
5SELECT * FROM movies WHERE title = 'The Kid'; # strings (match exact sequence)
ORDER BY
clause is used to sort data in ascending ASC
or descending DESC
orderASC
is default1# SELECT ___ FROM ___ ORDER BY ____ ASC|DESC;
2
3SELECT title
4FROM movies
5ORDER by duration; # get movie titles in (default) ascending order of duration (shortest first)
6
7SELECT title
8FROM movies
9ORDER by duration DESC; # get movie titles in descending order of duration (longest first)
>
, <
, =
, <=
, >=
, <>
<>
is same as !=
, it means not equal to1# SELECT ___ FROM ___ WHERE ____ > ____;
2
3SELECT * FROM movies WHERE duration > 100;
4SELECT * FROM movies WHERE duration < 100;
5SELECT * FROM movies WHERE duration >= 94;
AND
and OR
operators let’s you add multiple conditionsAND
to combine multiple conditions, the all conditions must be met to get any resultsOR
gives you results if any of the multiple conditions is met 1SELECT title
2FROM movies
3WHERE id = 1
4AND genre = 'Comedy'; # with AND, both conditions must be met
5
6
7SELECT title
8FROM movies
9WHERE id = 1
10OR genre = 'Comedy'; # will get results if either condition is met
INSERT
is the keyword for adding data.NULL
is the data type for when there’s no value in the cell (empty cell, missing data). NULL
is a placeholder for unknown data 1# INSERT INTO ___ (___, ___) VALUES (___, ___);
2# INSERT INTO tableName (columnName, columnName) VALUES (value, value);
3
4INSERT INTO movies (id, title, genre, duration)
5VALUES (5, 'The Circus', 'Comedy', 71);
6
7INSERT INTO movies
8VALUES (5, 'The Circus', 'Comedy', 71); # same as above since movies has only 4 columns
9
10INSERT INTO movies (title, duration)
11VALUES ('The Fly', 80)
UPDATE
is the keyword for updating existing dataWHERE
clause, the entire column will update for all rows 1# UPDATE ___ SET ___ = ___ (WHERE ___)
2# UPDATE tableName SET columnName = columnValue WHERE clause
3
4UPDATE movies
5SET genre = 'Romance'
6WHERE id = 5;
7
8UPDATE movies
9SET genre = 'Comedy', duration = 70 # update multiple values
10WHERE id = 5;
11
12UPDATE movies
13SET genre = 'Romance'
14WHERE id = 5 OR id = 7; # update multiple rows since id is unique
DELETE
is the keywordWHERE
can be used for further refining the statementWHERE
clause, it’ll delete all data from the table1# DELETE FROM ___ (WHERE ___)
2# DELETE FROM tableName (WHERE clause)
3
4DELETE FROM movies
5WHERE id = 5;
6
7DELETE FROM movies
8WHERE duration < 100;
CREATE DATABASE
and CREATE TABLE
are the self-explanatory keyword for creating databases and tablesDROP DATABASE
is when you want to remove a database (i.e. drop it) 1# CREATE DATABASE ___;
2CREATE DATABASE Chaplin Theaters;
3
4# DROP DATABASE ___;
5DROP DATABASE Chaplin Theaters;
6
7# CREATE TABLE ___ ( columnName dataType, columnNamedataType)
8CREATE TABLE movies
9(
10id int,
11title varchar(320),
12genre varchar(160),
13duration int
14);
15
16# DROP TABLE ___;
17DROP TABLE movies;
ALTER TABLE
keyword in conjunction with ADD COLUMN
and DROP COLUMN
1# ALTER TABLE ___ ADD COLUMN ___ ___;
2ALTER TABLE movies
3ADD COLUMN ratings int;
4
5# ALTER TABLE ___ DROP COLUMN ___;
6ALTER TABLE movies
7DROP COLUMN ratings;