reading-notes

Code Fellows Notes

View the Project on GitHub stephnitis/reading-notes

SQL Practice

SQLBolt

Lesson 1

To retrieve data from a SQL database, we need to write SELECT statements, which are often colloquially refered to as queries. A query in itself is just a statement which declares what data we are looking for, where to find it in the database, and optionally, how to transform it before it is returned.

Select query for specific columns:

SELECT column, another_column, …
FROM mytable;

If we want to retrieve absolutely all the columns of data from a table, we can then use the asterisk (*) shorthand in place of listing all the column names individually:

SELECT * 
FROM mytable;

Lesson One Complete

Lesson 2

In order to filter certain results from being returned, we need to use a WHERE clause in the query. The clause is applied to each row of data by checking specific column values to determine whether it should be included in the results or not.

Select query with constraints:

SELECT column, another_column, …
FROM mytable
WHERE condition
    AND/OR another_condition
    AND/OR …;

Operator and Condition Table

Lesson Two Complete

Lesson 3

Operator and Condition Table

Lesson Three Complete

Lesson 4

Select query with unique results:

SELECT DISTINCT column, another_column, …
FROM mytable
WHERE condition(s);

Select query with ordered results:

SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC;

Select query with limited rows:

SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC
LIMIT num_limit OFFSET num_offset;

Lesson Four Complete

Lesson 5: Review

Lesson Five Complete

Lesson 6

Select query with INNER JOIN on multiple tables:

SELECT column, another_table_column, …
FROM mytable
INNER JOIN another_table 
    ON mytable.id = another_table.id
WHERE condition(s)
ORDER BY column, … ASC/DESC
LIMIT num_limit OFFSET num_offset;

Lesson Six Complete

Lesson 13

Insert statement with values for all columns:

INSERT INTO mytable
VALUES (value_or_expr, another_value_or_expr, …),
       (value_or_expr_2, another_value_or_expr_2, …),
       …;

Insert statement with specific columns:

INSERT INTO mytable
(column, another_column, …)
VALUES (value_or_expr, another_value_or_expr, …),
      (value_or_expr_2, another_value_or_expr_2, …),
      …;

Insert statement with expressions:

INSERT INTO boxoffice
(movie_id, rating, sales_in_millions)
VALUES (1, 9.9, 283742034 / 1000000);

Lesson 13 Complete

Lesson 14

Update statement with values:

UPDATE mytable
SET column = value_or_expr, 
    other_column = another_value_or_expr, 
    …
WHERE condition;

Lesson 14 Complete

Lesson 15

Delete statement with condition:

DELETE FROM mytable
WHERE condition;

Lesson 15 Complete

Lesson 16

Create table statement with optional table constraint and default value:

CREATE TABLE IF NOT EXISTS mytable (
    column DataType TableConstraint DEFAULT default_value,
    another_column DataType TableConstraint DEFAULT default_value,
    …
);

Common Supported Data Types:

Table of Supported Data Types

A Few Common Constraints:

Table of Common Constraints

Lesson 16 Complete

Lesson 17

Altering table to add new column(s):

ALTER TABLE mytable
ADD column DataType OptionalTableConstraint 
    DEFAULT default_value;

Altering table to remove column(s):

ALTER TABLE mytable
DROP column_to_be_deleted;

Altering table name:

ALTER TABLE mytable
RENAME TO new_table_name;

Lesson 17 Complete

Lesson 18

Drop table statement:

DROP TABLE IF EXISTS mytable;

Lesson 18 Complete

Learn SQL