Unit 5 - SQL (Part 2) Flashcards

1
Q

How to rename headers in the results/output?

A

Use the ‘AS’ keyword:

SELECT CustomerName AS Name

^^ renames header/attribute in the results / output

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

How to rename an entire table (in the scope of the query only)?

A

SELECT *
FROM Customers AS C1;

^^ renames customers table to c1 inside this query only, so easier to pull data (e.g., SELECT C1.Name, WHERE C1.age > 30, etc.)

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

What are the two main ways to join tables in SQL?

A
  1. Use the ‘WHERE’ clause
  2. Use Inner and Outer JOIN Operators
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When can you use the ‘WHERE’ keyword to join tables?

A

When the tables have common attributes (even if attribute name is different)

SELECT *
FROM Table1, Table2
WHERE Table1.X = Table2.Y;

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

What’s more common to see: ‘WHERE’ or ‘INNER JOIN’?

A

INNER JOIN - better readability

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

What’s the syntax for INNER JOIN? Say you want to join customers with orders (common attribute customer_id)

A

FROM customers AS C
INNER JOIN orders AS O on C.customer_id = O.customer_id;

This will display all columns from customers and orders,
and all the rows/records where customer_id in orders is the same as customer_id in customers

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

LEFT JOIN syntax

A

FROM customers AS C
LEFT JOIN orders AS O on C.customer_id = O.customer_id;

This will include ALL rows from customers and only the corresponding rows from orders

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

FULL JOIN syntax

A

MySQL does not support FULL JOIN. You can get this result by doing the union of LEFT JOIN and RIGHT JOIN

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

Does MySQL support NATURAL JOIN?

A

Yes, but DO NOT USE - AVOID IT!
That’s because there may be MULTIPLE common columns, it just assumes which ones you want - you may not get the result you want.

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

What are the rules with using UNION, INTERSECT, EXCEPT? What’s the syntax?

A

The two tables must be compatible (same attribute names, same data types, same number of each)

SELECT column_name(s)
FROM table1

OPERATOR

SELECT column_name(s)
FROM table2;

^^ column_name(s) must be compatible

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

What’s the syntax for EXCEPT operator?

A

MySQL doesn’t support EXCEPT, must use NOT IN

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

What are the aggregate functions?

A

MIN(), MAX(), COUNT(), AVG(), SUM()

They’re applied to a single column/ attribute to return a single value
e.g., MIN(gpa) = returns the smallest value from the column gpa

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

What is COUNT(*)?

A

Special case of COUNT() where you it counts all the rows of a table regardless of whether null or duplicate values occur

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

Where can you use an aggregate function (what part of the SQL query)?

A

ONLY two spots:
1. SELECT list
2. HAVING clause

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