learning sql

Resource from codecademy.

1
2
3
4
5
CREATE TABLE table_nam(
column_1, data_type,
column_2, data_type,
column_3, data_type
);

Create

1
2
3
4
5
CREATE TABLE celebs (
id INTEGER,
name TEXT,
age, INTEGER
);
  1. CREATE TABLE clause tells SQL that you want to create a new table
  2. celebs name of the table
  3. (id INTEGER, name TEXT, age INTEGER) a list of parameters defining each column in the table and its data type
  • id is the first column in the table.It stores values of data type INTEGER
  • name stores values of data type TEXT
  • age stores values of data type INTEGER

INSERT

1
INSERT INTO celebs (id, name, age) VALUES (1, 'William Turner');

This statement inserts new rows into a table. You can use the INSERT statement when you want to add new records.

  1. INSERT INTO clause adds the specified row/rows
  2. celebs name of the table the row is added to
  3. (id, name, age) is a parameter identifying the columns that data will be inserted into
  4. VALUES indicates the data being inserted
  5. (1, 'William Turner, 21') is a parameter identifying the values being inserted
  • 1 insert into id column
  • William Turner inserted into name column
  • 21 inserted into age column

SELECT

1
SELECT name FROM celebs;

SELECT statements are used to fetch data from a database. Here, SELECT returns all data in the name column of the celebs table.

  1. SELECT clause indicates that the statement is a query
  2. name specifies the column to query data form
  3. FROM celebs specifies the name of the table to query data from

Query data from all columns in a table with SELECT.

1
SELECT * FROM celebs;

* allows to select every column in a table without having to name each one individually.

SELECT statements always return a new table called the result set.

UPDATE

1
UPDATE celebs SET age = 22 WHERE id = 1;

UPDATE statement edits a row in the table. To change existing records.

  1. UPDATE edits a row in the table
  2. celebs table name
  3. SET indicates the column to edit
  4. WHERE indicates which row(s) to update with the new column value.

ALTER

1
ALTER TABLE celebs ADD COLUMN twitter_handle TEXT;

The ALTER TABLE statement added a new column to the table.

  1. ALTER TABLE lets you make the specified changes
  2. celebs name of table
  3. ADD COLUMN lets you add a new column to a table
    1. twitter_handle name of new column being added
    2. TEXT data type
  4. NULL is a special value in SQL that represents missing or unknown data.

DELETE

1
DELETE FROM celebs WHERE twitter_handle IS NULL;

DELETE FROM statement deletes one or more rows from a table.

  1. DELETE FROM lets you delete rows from a table
  2. WHERE lets you select which rows you want to delete.
  3. IS NULL a condition in SQL that returns true when the value is NULL and false otherwise