SQL MYSTERY TUTORIAL Flashcards

1
Q

What does SQL stand for and what does it do?

A

Structured Query Language

It is a way to interact w/ relational databases and tables that allow humans to glean specific, meaningful information

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

What is a relational database?

A

Best known type of database, but No single definition for word database
Can have varying amount of structure imposed on data
“Relational” part comes w/ specific rules about how to connect data btw different tables

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

What are relational databases made of? Describe what they look like.

A

Made of tables, like spreadsheets
Each column in table has name and data type (text, number)
Each row in the table is a specific instance of what the table is about

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

What does ERD stand for? What is an ERD?

A

Entity relationship diagram

Visual representation of the relationships among all relevant tables w/in a database

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

Describe the ERD

A

Diagram that shows each table, comprised of a name, column names, & data types

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

Describe the gold key, blue arrow and gray arrows on ERD

A

*Gold key indicates the column is the primary key of the corresponding table

*Blue arrow: indicates the column is the foreign key of the corresponding table

*Gray arrow: if two tables are related, the matching columns, i.e., the common identifiers of the two tables, are connected by gray arrow

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

What is a primary key?

A

A unique identifier for each ROW in a table

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

What is a foreign key?

A

Used for reference data in one table to those in another table

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

What is a query?

A

Statements constructed to get data from database

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

Describe trying a query against the database

A
  1. For each boxes below, click the “run” to “execute” the query in the box
  2. You can edit the queries on the page to explore (not case sensitive, but conventional to capitalize for readability; you can use new lines and white spaces to format command for readability).
  3. Most database systems require to end query in semicolon (;)
  4. You can change subject of FROM to any other table from the ERD to learn how many rows that table has
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Part 2 of Describing query against database
What do we know about the subject/if you want data for each row in a table, use what?

A

Use * after ‘SELECT’ Rather than seeing all of them, limit results to first 10
SELECT * FROM person LIMIT 10;

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

Part 3 of describing a query against a database
What are possible values for a column?

When working w/ data, always see if you can find documentation that explains the _____. Is this always available?

What does the DISTINCT keyword do?

A

explains the database structure (like the ERD) and valid values. But not always available

DISTINCT keyword can give you a quick look at which values are in the database.

After you run it, delete the word DISTINCT and run it again. (after you try that click “reset” and run one more time before continuing)

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

What elements does a SQL query have? (6 main ones)

A

*SQL keywords (ex- SELECT, FROM)
*Column names (like name column)
*Table names (ex- person table)
*Wildcard characters (ex- %)
*Functions
*Specific filtering criteria

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

What are SQL keywords used for?

A

*Used to specify actions in your queries
*not case sensitive, but use all caps to easily identify

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

What are some frequently used SQL keywords and what are they used for?

A

SELECT- allows grabbing of data from specific columns from the database

FROM- allows us to specify which table(s) we care about

WHERE- clause in a query that is used to filter results by specific criteria

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

Describe the SELECT SQL keyword

A

allows grabbing of data from specific columns from the database
- * (asterisk): used after SELECT to grab all column from table
- column_name(s): to select specific columns, put the names of the columns after SELECT and use commas to separate them

17
Q

Describe the FROM SQL keyword

A

Allows us to specify which table(s) we care about; to select multiple tables, use the table names and commas to separate them

18
Q

Describe the WHERE SQL keyword

A

Used to filter results by specific criteria
SELECT * FROM person WHERE name = ‘Kinsey Erickson’
Note that you need to use single straight quotes (‘) around literal text so the database can tell it apart from table and column names

The ​AND​ keyword is used to string together multiple filtering criteria so that the filtered results meet each and every one of the criteria. (There’s also an OR keyword, which returns rows that match any of the criteria.)

19
Q

True or False: when querying for text values, you must match the data as it is in the database.

A

True, if it doesn’t match your query may come up “no data found”

Notice that when querying for text values, you must match the data as it is in the database. Try changing ‘Chicago’ to ‘chicago’ and running the statement. Then, see if you can edit this SQL statement to find the first clue based on the prompt above.

20
Q

How does SQL handle when you only know part of the information you need?

A

Wildcards: special symbols that represent unknown characters, SQL supports two wildcards

21
Q

What’s the most common wildcard? and describe it

A

%: when you place a % wildcard in a query string, SQL system will return results that match the rest of the string exactly, and have anything (or nothing) where the wildcard is
ex) or example, ‘Ca%a’ matches Canada and California.

22
Q

What’s the least commonly wildcard? and what does it mean?

A

_ : it means ‘match the rest of the text, as long as there’s exactly one character in exactly the position of the _, no matter what it is

So, ‘B_b’ would match ‘Bob’ and ‘Bub’ but not ‘Babe’ or ‘Bb’.

23
Q

When using wildcards, which symbol don’t you use? And which one do you use instead?

A

You don’t use the = symbol; use the LIKE word

24
Q

Trying out wildcards

A

Try out some wildcards.
Once you run this command, try variations like ‘Irvin_’ and ‘I%e’ – and then explore some more.

1 SELECT DISTINCT city
2 FROM crime_scene_report
3 WHERE city LIKE ‘I%e’;