w2d6-7 (SQL Intro) Flashcards

1
Q

What is an RDMBS?

A

A relational database management system.

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

What do the rows and columns of a database table represent?

A

Each row represents a single entity, and each column denotes a specific piece of data for that entity.

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

What is a primary key, and what is the most common one in SQL?

A

A primary key is the unique identifier for a row of data, and the most typical one is ‘id’

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

In Rails, what does each table usually hold?

A

One type of resource (users, people, houses, posts, etc.)

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

What is a database schema, and what must you decide to implement it?

A

A database schema is a description of the organization of your database into tables and columns. You must decide what tables to have, what columns each table will have, and the data type of each column.

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

What is the difference between static and dynamic typing?

A

Static typing requires the data type of a variable be specified and immutable (SQL), whereas this is not a requirement in dynamic typing (Ruby)

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

What is a foreign key?

A

An entry in a table which points to a row (primary key) in a different table; used for associations between tables.

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

What is the convention for naming a foreign key?

A

[other_table_name_singularized]_id

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

What do the ‘SELECT’, ‘FROM’, and ‘WHERE’ clauses do?

A

SELECT takes a list of comma separated column names, FROM specifies a table to query, and WHERE specifies which rows to select depending on certain values of their columns (i.e. filters the data).

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

What are the 4 main data manipulation operations in SQL?

A

SELECT, INSERT, UPDATE, DELETE

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

What are the 3 operators to manipulate a database schema?

A

CREATE TABLE, ALTER TABLE, DROP TABLE

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

What does the ‘JOIN’ clause do?

A

It allows us to query across tables by associating a foreign key with a primary id.

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

What are the two components of the SQL language?

A

Data Definition Language (DDL), and Data Manipulation Language (DML)

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

Explain an INNER JOIN, FULL OUTER JOIN, and LEFT OUTER JOIN

A

An inner join produces a table consisting of rows where a certain column value matches.

A full outer join produces a set of all records where a certain column value matches. If there is no match, the record in the other table will contain null.

A left outer join does the samething, except all records are retained in the first table, and only matching records in the second table are retained.

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

Write the SQL query to join TableA and TableB on their name column value and then exclude all matching results.

A
SELECT * FROM TableA
FULL OUTER JOIN TableB
ON TableA.name = TableB.name
WHERE TableA.id IS null
OR TableB.id IS null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are good SQL naming conventions as they relate to Rails?

A

Name files in snake_case and pluralize them. Also, use the ‘id’ column as the primary key for each table.

17
Q

What is a database connection?

A

A connection between the user/application and the SQL server. It is held until one of the two parties breaks the connection.

18
Q

What is a query optimizer?

A

A part of the server which takes a properly formed query and determines the most efficient way to execute it.

19
Q

Explain the SELECT, FROM, WHERE, GROUP BY, HAVING, and ORDER BY clauses.

A

Select Determines which columns to include in the query’s result set

From Identifies the tables from which to draw data and how the tables should be joined

Where Filters out unwanted data

Group by Used to group rows together by common column values

Having Filters out unwanted groups

Order by Sorts the rows of the final result set by one or more columns

20
Q

T/F: SQL queries are ternimated by a ‘;’

A

T

21
Q

How do yo add a column alias to a SELECT clause?

A

Add the alias after the corresponding element in the SELECT clause:

mysql> SELECT emp_id, -> ‘ACTIVE’ status, -> emp_id * 3.14159 empid_x_pi, -> UPPER(lname) last_name_upper -> FROM employee;

22
Q

How do you remove duplicate returns from a SELECT clause?

A

Add the ‘DISTINCT’ keyword:

SELECT DISTINCT cust_id FROM account;

23
Q

What is a subquery?

A

A query within a query, which can be used to further narrow down results.

24
Q

What is the syntax for a subquery?

A

Enclose it in parentheses and reference it by an alias (‘e’ below):

SELECT emp_id FROM (SELECT fname FROM employee) e;

25
Q

What is a view?

A

A table generated by a select statement that is temporarily stored as a variable and can be queried:

mysql> CREATE VIEW employee_vw AS -> SELECT emp_id, fname, lname, -> YEAR(start_date) start_year -> FROM employee;

26
Q

How do you define a table alias?

A

In the FROM clause:

SELECT e.emp_id, e.fname, e.lname, d.name dept_name FROM employee AS e INNER JOIN department AS d ON e.dept_id = d.dept_id;

Note that the alias is assigned in the FROM clause but used in the SELECT and WHERE clauses (FROM is evaluated first, then WHERE, then SELECT, which is why this works). Also, note that the ‘AS’ keyword is optional.

27
Q

What are the primary logical operators for the WHERE clause? How can they be grouped?

A

AND, OR, and NOT, and they can be grouped with parentheses:

mysql> SELECT emp_id, fname, lname, start_date, title -> FROM employee -> WHERE (title = ‘Head Teller’ AND start_date > ‘2006-01-01’) -> OR (title = ‘Teller’ AND start_date > ‘2007-01-01’);

28
Q

How do you order across multiple columns?

A

Separate the orderings by parentheses:

mysql> SELECT open_emp_id, product_cd -> FROM account -> ORDER BY open_emp_id, product_cd;

29
Q

What is the keyword to add to the end of an ORDER BY clause to specify ascending or descending order?

A

ASC (default) and DESC

30
Q

How do you sort via an expression?

A

Add it after ORDER BY:

mysql> SELECT cust_id, cust_type_cd, city, state, fed_id -> FROM customer -> ORDER BY RIGHT(fed_id, 3);