🔗 01_introduction.sql
🔗 02_TheSelect.sql
-
SELECTretrieves data from a database. -
*asterisk means all columns. -
FROMkeyword followed by the table name. -
showsis the name of the table we are requesting data from. -
;we end the statement with a semicolon. -
Can have two forms:
SELECT * FROM shows;
SELECT *
FROM shows;-
DISTINCTis used to return just the unique values in a column, so no duplicates -
Will show all (repeated too)
SELECT genre
FROM shows;- Without repeating
SELECT DISTINCT genre
FROM shows;-
WHEREfilters information based on a confidition -
=equal to -
!=not equal to -
>greater than -
<less than -
>=greater than or equal to -
<=less than or equal to -
Presents information of id = 23
SELECT *
FROM shows
WHERE id = 23;- Presents shows listed after 2020
SELECT *
FROM shows
WHERE year > 2020;- The
LIKEoperator can be used to search for a pattern in a column - The percentage sign
%is a wildcard character that can be used withLIKEA%matches values that begin with letter 'A'.%zmatches values that end with 'z'.
SELECT *
FROM shows
WHERE name LIKE '%the%';- The
BETWEENoperator is used in aWHEREclause to filter the result set within a certain range - The range must be separated by an
ANDkeyword - When the values are text,
BETWEENfilters the result within the alphabetical range.
SELECT *
FROM shows
WHERE year
BETWEEN 2020 AND 2025;
-- Presents shows with names that begin with A up to D
SELECT *
FROM shows
WHERE name
BETWEEN 'A' AND 'D';-
The
ORDER BYstatement sorts rows of data in ascending or descending order. -
By default, this command sorts the data in ascending order.
-
Default into showing ascending order
SELECT name, genre, stream, year
FROM shows
ORDER BY year;
SELECT name, genre, stream, year
FROM shows
ORDER BY year DESC;SELECTselects dataFROMa database.SELECT *selects all the columns.DISTINCTreturns unique values in a column.WHEREfilters results based on a condition.- Comparison operators:
=,!=,>,<,>=,<=. LIKEoperator searches for a specific pattern.BETWEENoperator matches values in a range.ORDER BYsorts data (ascending/descending).
- The
COUNT()function counts the number of rows
SELECT COUNT(*)
FROM table_name;*counts every row
MIN()andMAX()are exactly how they sound: they return the minimum and maximum value in a column, respectively
SELECT MIN(plays)
FROM playlist;
SELECT title, artist, MAX(plays)
FROM playlist;- The
SUM()aggregate function takes a column and returns the total sum of the values in it
SELECT SUM(plays)
FROM playlist;- Use the
AVG()function to calculate the average value of a column
SELECT AVG(plays)
FROM playlist;- The
GROUP BYstatement groups rows of data with the same values into buckets. - It’s often used with aggregate functions to group the result by one or more columns.
SELECT genre, COUNT(*)
FROM playlist
GROUP BY genre;
SELECT genre, AVG(duration)
FROM playlist
GROUP BY genre;COUNT()returns the number of rowsMIN()returns the smallest value in a columnMAX()returns the largest value in a columnSUM()returns the total sum in a columnAVG()returns the average value in a columnGROUP BYgroups the result using the common values in a column
CREATE TABLEstatement creates a brand new table in a database
CREATE TABLE companies (
id INTEGER,
name TEXT,
headquarters TEXT,
year INTEGER
);idcolumn of the data typeINTEGERnamecolumn of the data typeTEXTheadquarterscolumn of the data typeTEXTyearcolumn of the data typeINTEGER
TEXT: a text stringINTEGER: a positive or negative whole numberREAL: a positive or negative decimal numberDATE: a date format (YYYY-MM-DD)
- Use the
INSERTstatement to add a new row into a table
INSERT INTO companies (id, name, headquarters, year)
VALUES (1, 'Twitter', 'San Francisco', 2006);INSERT INTOis a clause that adds the specified row- companies the name of the table the row is being added to
(id, name, headquarters, year)is a parameter with the column names that data will be inserted toVALUESclause indicates the data being inserted(1, 'Twitter', 'San Francisco', 2006)are the values
Example:
INSERT INTO companies (id, name, headquarters, year)
VALUES (1, 'Twitter', 'San Francisco 🌁', 2006);
INSERT INTO companies (id, name, headquarters, year)
VALUES (2, 'Duolingo', 'Pittsburgh 🐝', 2011);
INSERT INTO companies (id, name, headquarters, year)
VALUES (3, 'BeReal', 'Paris 🇫🇷', 2020);
INSERT INTO companies (id, name, headquarters, year)
VALUES (4, 'Codedex', 'New York 🗽', 2022);ALTER TABLE/ADD COLUMNstatement adds a new column to an existing table
ALTER TABLE companies
ADD COLUMN about TEXT;- The
UPDATEstatement edits a row in a table.
UPDATE companies
SET name = 'X'
WHERE name = 'Twitter';
UPDATE companies
SET headquarters = 'Brooklyn 🌉'
WHERE id = 4;DELETE FROMstatement removes one or more rows from a table
DELETE FROM companies
WHERE name = 'BeReal';CREATE TABLEadds a new tableINSERT INTOadds a new rowALTER TABLE/ADD COLUMNadds a new columnUPDATE/SETupdates a rowDELETE FROMdeletes a row(s)
*to be continued when the lecture is complete