SQL Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is SQL

A

Structured Query Language

IBM started out SQL as SEQUEL (Structured English QUEry Language) in the 1970s to query databases.

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

What is a database

A

A set of data stored in a computer

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

What is a relational database

A

A type of database that lets us store and identify data in relation to other data; often organized into tables

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

What are tables made of

A

Rows (records) and columns (which have names and data types)

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

What is an RDMS

A

Relational Database Management System; lets you create, update, and administer a relational database
Most use SQL to access the database

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

What is SQLite

A

A RDMS that uses minimal SQL

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

What are five popular RDMSs

A

MySQL, PostgreSQL, Oracle DB, SQL Server, SQLite

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

Where is a glossary of SQL commands

A

https://www.codecademy.com/articles/sql-commands

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

ALTER TABLE

A

ALTER TABLE table_name
ADD column_name datatype;

adds column to a table in a database

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

What are some of the most common data types in relational databases

A

INTEGER
TEXT
DATE YYYY-MM-DD
REAL (decimal value)

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

What are a statement and its components

A

Text a database recognizes as a valid command

clauses/commands: written in caps
table name to which the clause applies
parameter: list of columns, data types, or values that are passed into the clause as an argument

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

What do SELECT statements return

A

A table

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

What are constraints

A

add info about how a column can be used; are added after data type; tells database to reject data that does not conform

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

CREATE TABLE

A

creates a new table

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

INSERT INTO

A

adds a new row to a table

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

SELECT

A

queries data from a table

17
Q

UPDATE

A

UPDATE table_name
SET some_column = some_value
WHERE some_column = some_value;

edits a row in a table

18
Q

DELETE FROM

A

deletes rows from a table

19
Q

what is the purpose of *

A

selects every column in the table

20
Q

SELECT DISTINCT

A

SELECT DISTINCT column_name
FROM table_name;

specifies that the statement is going to be a query that returns unique values in the specified column(s).

21
Q

LIKE

A

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;

LIKE is a special operator used with the WHERE clause to search for a specific pattern in a column.

_ is wildcard
% is a wildcard character that matches zero or more missing letters in the pattern.

you can apply the LIKE operator to numerical values as well. Whenever you use LIKE however, you must always wrap the pattern within a pair of quotations, whether for matching a number or a string.

22
Q

BETWEEN

A

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value_1 AND value_2;

The BETWEEN operator is used to filter the result set within a certain range. The values can be numbers, text or dates.

is inclusive (which works kinda differently for integers and text?)

23
Q

ORDER BY

A

SELECT column_name
FROM table_name
ORDER BY column_name ASC | DESC;

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

24
Q

LIMIT

A

SELECT column_name(s)
FROM table_name
LIMIT number;

LIMIT is a clause that lets you specify the maximum number of rows the result set will have.

not supported in all SQL databases

25
Q

CASE

A
SELECT column_name,
  CASE
    WHEN condition THEN 'Result_1'
    WHEN condition THEN 'Result_2'
    ELSE 'Result_3'
  END
FROM table_name;

CASE statements are used to create different outputs (usually in the SELECT statement). It is SQL’s way of handling if-then logic.

26
Q

What is an aggregate

A

a calculation performed on multiple rows of a table

computes a single result set from a set of values

27
Q

COUNT ()

A

takes the name of a column as an argument and counts the number of non-empty values in that column.

28
Q

SUM ()

A

takes the name of a column as an argument and returns the sum of all the values in that column.

29
Q

If multiple rows have the minimum or maximum value, which one is returned when using MAX/MIN?

A

Typically, when you have more than one row that contains the minimum or maximum value in a column, the topmost row containing that value will be returned in the result.

30
Q

In SQL, how can we get the average of only the unique values of a column?

A

To run the AVG() function on a column such that it only averages the unique values in the column, we could use the DISTINCT clause right before the column name.

SELECT AVG(DISTINCT column)

31
Q

GROUP BY

A

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.

32
Q

HAVING

A

HAVING is very similar to WHERE. In fact, all types of WHERE clauses you learned about thus far can be used with HAVING.

When we want to limit the results of a query based on an aggregate property, use HAVING.

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

33
Q

inner join vs left join

A

inner join: results only includes rows that match on condition

left join: keeps all rows from first table, regardless of matches from second

34
Q

primary key vs foreign key

A

primary key:
not null
unique
only one primary key column per table

foreign key:
another table’s primary key in a different table

35
Q

cross join

A

join all the columns!

36
Q

union

A

stacks tables, which must have:
same number of columns, with same data types in same order

When you combine tables with UNION, duplicate rows will be excluded.

To explain why this is the case, recall a Venn Diagram, which shows the relations between sets. If we perform UNION on two sets of data (tables), say A and B, then the data returned in the result will essentially be
A + B - (A intersect B)

In the first part,
A + B
will add together all the rows of both tables, including duplicates.

The second part,
- (A intersect B)
will remove every duplicate, which is where A and B intersected.

If, however, you wanted to include duplicates, certain versions of SQL provides the UNION ALL operator.

37
Q

with

A

with xx_alias as (entireQuery)

Query