SQL for non-programmers part 2 Flashcards

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

Which SQL clause is used to filter data, retrieving only the rows of data that fulfill a specific condition?
LIMIT
SELECT
WHERE
ORDER BY

A

WHERE allows you to specify conditions that must be met for a row to be included in your result set, essentially applying a filter over the data

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

What are the most common aggregate functions? Why use them?

A

COUNT
SUM
AVG
MIN
MAX
Helps us analyze data without having to extract data row by row from a table

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

What kind of syntax do aggregate functions need to follow?

A

The aggregate function needs to follow before the column name of the one you want to perform the function ona and the column name should have parentheses.

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

Why use alias
AS

A

To name a column something shorter when it has a long complicated name

Ex:
SELECT COUNT (DISTINCT cust_id)
FROM oliveoil.sales

change to
SELECT COUNT (DISTINCT cust_id) AS customer_count
FROM oliveoil.sales;
–this will return a result on the grid view with that given title and the number result

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

What does the SUM function do? What about AVG function?

A

It adds all numerical values in a column
The AVG will get an average of all numbers in the column selected

Ex:
SELECT SUM (order_total) AS sum_of_order
AVG (order_total) AS avg_order
FROM oliveoil.sales

This will return in the grid view, the numbers for both sum_of_order and avg_order

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

Use GROUP BY when

A

Grouping can be used to divide data into groups and then perform aggregate calculations on those groups. GROUP BY must be placed after the WHERE clause and before ORDER BY

Ex:
WHERE
GROUP BY
ORDER BY

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

What is the HAVING clause?

A

Used to filter the rest of a GROUP BY query based on specific conditions
It is placed after GROUP BY and before ORDER BY
Ex:
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
LIMIT

Filters groups, dont confuse with WHERE that’s used to filter rows

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

How does HAVING clause differ from a WHERE clause?

A

HAVING clause is used to filter groups of data after they have been formed. Contrary to a WHERE clause which filters data rows before any groups have been applied.

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

What is an equijoin?

A

SELECT, FROM and WHERE
Joins two or more related data tables
It belongs to the broader INNER JOIN Family

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

INNER JOIN

A

A classic inner join is preferred in SQL community and is more robust, flexible option, useful as queries become more complex

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

What is the difference between joins?

A

The difference between joins lies in which rows you want to return from your selected data tables

INNER JOIN returns all of the rows that match the join conditions

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

What do you do if you wanted to return the rows that don’t match the join conditions?
Or all of the rows from one table and only those that match from another table?

A

LEFT JOIN returns all of the rows from the left table and only the matching rows from the right table

The left table is the table preceeding the JOIN keyword in your SQL code and the right table is the table immediately after the JOIN keyword

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

What does the RIGHT JOIN command do in SQL?

A

It does the opposite of LEFT JOIN. It returns all of the rows from the right table and only the matching rows from the left table

This is not supported in SQLite but it can be achieved by swapping the order of the tables listed on your JOIN clause

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

What is a CROSS JOIN?

A

It creates a cartesian product of rows from joined tables. A cartesian product means that it combines and returns all possible combinations of rows from both tables. You do not need to use ON condition because it includes everything regardless of a match.

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