SQL Flashcards
What do SQL statements always end with?
Semi colon (;)
In this statement. What would you put in place of the #, * and the +?
CREATE TABLE # (
* +,
* +
);
# = Table Name * = Column Name \+ = Date Type
In this statement. What would you put in place of the #, * and the +?
INSERT # celebs (*)
VALUES (+);
# = INTO * = Column Name \+ = Value being inserted
In this statement. What would you put in place of the # and the +?
SELECT # FROM +;
# = Column name or * for all columns in table \+ = Table name
What is the clause that updates rows in a table?
UPDATE {Table name} SET {column name = new value} WHERE {column name = known value}
How do you add columns to a table?
Eg.
ALTER TABLE celebs
ADD COLUMN twitter_handle TEXT;
The ALTER TABLE statement added a new column to the table. You can use this command when you want to add columns to a table.
- ALTER TABLE is a clause that lets you make the specified changes.
- celebs is the name of the table that is being changed.
- ADD COLUMN is a clause that lets you add a new column to a table.
twitter_handle is the name of the new column being added
TEXT is the data type for the new column
What does NULL represent?
NULL is a special value in SQL that represents missing or unknown data.
When creating a table, constraints can be specified on the data. What does applying the PRIMARY KEY, UNIQUE, NOT NULL and DEFAULT constraints do?
Statement Example: CREATE TABLE celebs ( id INTEGER PRIMARY KEY, name TEXT UNIQUE, date_of_birth TEXT NOT NULL, date_of_death TEXT DEFAULT 'Not Applicable', );
PRIMARY KEY columns can be used to uniquely identify the row. Attempts to insert a row with an identical value to a row already in the table will result in a constraint violation which will not allow you to insert the new row.
UNIQUE columns have a different value for every row. This is similar to PRIMARY KEY except a table can have many different UNIQUE columns.
NOT NULL columns must have a value. Attempts to insert a row without a value for a NOT NULL column will result in a constraint violation and the new row will not be inserted.
DEFAULT columns take an additional argument that will be the assumed value for an inserted row if the new row does not specify a value for that column.
SQL is a programming language designed to manage and manipulate data stored in what kind of database?
Relational databases
What does AS do?
AS is a keyword in SQL that allows you to rename a column or table using an alias. The new name can be anything you want as long as you put it inside of single quotes.
What does DISTINCT do?
DISTINCT is used to return unique values in the output. It filters out all duplicate values in the specified column(s).
e.g. SELECT DISTINCT
What statement would you use if you wanted to select only rows from a table called movies where the genre was ‘Comedy’
SELECT *
FROM movies
WHERE genre = ‘Comedy’;
What character can be used with LIKE to represent zero or more missing characters from a pattern
Statement example:
SELECT *
FROM movies
WHERE name LIKE ‘?A’
%
When using BETWEEN in SELECT queries, for what value is the 2nd input inclusive and not inclusive
Inclusive - Numbers
Not Inclusive - Letters
What operator is used to link multiple conditions for a SELECT query
AND