SQL Flashcards

1
Q

What do SQL statements always end with?

A

Semi colon (;)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In this statement. What would you put in place of the #, * and the +?

CREATE TABLE # (
* +,
* +
);

A
#  = Table Name
* = Column Name
\+ = Date Type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In this statement. What would you put in place of the #, * and the +?

INSERT # celebs (*)
VALUES (+);

A
# = INTO
* = Column Name
\+ = Value being inserted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In this statement. What would you put in place of the # and the +?

SELECT # FROM +;

A
# = Column name or * for all columns in table
\+ = Table name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the clause that updates rows in a table?

A

UPDATE {Table name} SET {column name = new value} WHERE {column name = known value}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you add columns to a table?

A

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.

  1. ALTER TABLE is a clause that lets you make the specified changes.
  2. celebs is the name of the table that is being changed.
  3. 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does NULL represent?

A

NULL is a special value in SQL that represents missing or unknown data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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',
);
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

SQL is a programming language designed to manage and manipulate data stored in what kind of database?

A

Relational databases

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does AS do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does DISTINCT do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What statement would you use if you wanted to select only rows from a table called movies where the genre was ‘Comedy’

A

SELECT *
FROM movies
WHERE genre = ‘Comedy’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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’

A

%

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When using BETWEEN in SELECT queries, for what value is the 2nd input inclusive and not inclusive

A

Inclusive - Numbers

Not Inclusive - Letters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What operator is used to link multiple conditions for a SELECT query

A

AND

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What operator is used to search for different conditions for a SELECT query

A

OR

17
Q

What does ORDER BY do and what are the key words to order by?

A

ORDER BY is a clause that indicates you want to sort the result set by a particular column.

ASC is used to order ascending
DESC is used to order descending

**Note ORDER by always goes after WHERE if present

18
Q

What is used to handle if-when login in SQL?

A

A CASE statement allows us to create different outputs (usually in the SELECT statement).

eg 
SELECT name,
 CASE
  WHEN imdb_rating > 8 THEN 'Fantastic'
  WHEN imdb_rating > 6 THEN 'Poorly Received'
  ELSE 'Avoid at All Costs'
 END
FROM movies;

Each WHEN tests a condition and the following THEN gives us the string if the condition is true.
The ELSE gives us the string if all the above conditions are false.
The CASE statement must end with END

19
Q

What does this statement do?

SELECT COUNT(*)
FROM table_name;
A

Counts the number of rows in a table

20
Q

What does this statement do?

SELECT SUM(downloads)
FROM fake_apps;
A

SUM() is a function that takes the name of a column as an argument and returns the sum of all the values in that column.

21
Q

How would you find the minimum or maximum value in a column?

A

SELECT MAX/MIN(column)

FROM table_name;

22
Q

How would you find the average of a column?

A
SELECT AVG(column)
FROM table_name;
23
Q

What are the arguments input into the ROUND function?

A

ROUND() function takes two arguments inside the parenthesis:

  1. a column name
  2. an integer (i.e. 0 for 0 decimal places)
24
Q

What does GROUP BY do?

A

GROUP BY is a clause in SQL that is used with aggregate functions. It is used in collaboration with the SELECT statement to arrange identical data into groups.

The GROUP BY statement comes after any WHERE statements, but before ORDER BY or LIMIT.

25
Q

What class do you use to filter groups of data?

A

HAVING

eg. SELECT year,
   genre,
   COUNT(name)
FROM movies
GROUP BY 1, 2
HAVING COUNT(name) > 10;

HAVING statement always comes after GROUP BY, but before ORDER BY and LIMIT.

26
Q

How do you link two tables by an id?

A

Using a table JOIN

e.g. 
SELECT *
FROM orders
JOIN customers
  ON orders.customer_id = customers.customer_id;
27
Q

What does a LEFT JOIN do?

A

A left join will keep all rows from the first table, regardless of whether there is a matching row in the second table.

The final result will keep all rows of the first table but will omit the un-matched row from the second table.

28
Q

What is a CROSS JOIN and what is its main use?

A

A CROSS JOIN combines all rows of one table with all rows of another table.

The most common usage of CROSS JOIN is when we need to compare each row of a table to a list of values.

29
Q

What does UNION do and what criteria must be filled?

A

UNION ‘stacks’ two tables together

  • Tables must have the same number of columns.
  • The columns must have the same data types in the same order as the first table.
30
Q

What do you use to create a temporary table to query?

A

WITH allows us to define a bunch of temporary tables that can be used in the final query.