SQL Basics Flashcards

(45 cards)

1
Q

SELECT

A

Column you want to look at

use to specify the columns you want to retrieve data from

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

FROM

A

Table the data lives in

used to specify the table or tables you want to retrieve data from

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

WHERE

A

Certain conditions on the data

used to filter data based on specific conditions

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

GROUP BY

A

Column you want to aggregate by

used to group data based on one or more columns

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

ORDER BY

A

Column you want to order results by in ASC or DESC order

Used to sort data in ascending or descending order based on one or more columns. The ordering is automatically ASC and will order in sequence of your first statement.

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

SELECT DISTINCT

A

Returns unique values of specified columns. This can be useful to eliminate duplicate rows from your query results.

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

INNER JOIN

A

When you only want to see information from two tables that match.

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

HAVING

A

Certain condition on aggregation

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

LIMIT

A

Maximum order of rows you want your results to contain.

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

Syntax for INNER JOIN

A

SELECT Columns
FROM Table 1 INNER JOIN table 2
ON Table1 Col 1 = Table 2 col 2

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

Joins - USING instead of ON

A

USING has to be exact match, ON Specifys columns

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

Aliases and syntax

A

Aliases shorten names when you are refering to them in SQL

SELECT c.col1, c.col2, e.col1, e.col2

FROM Customer c INNER JOIN e
ON c.ID = e.ID

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

If you have to count the number of times a track appears in the table and group by track name. What would the statement be?

Table = Track
Track Name = Name

A

– Select the track name and count
SELECT t.Name, COUNT(*)

FROM Track t

– Filter on the USA
WHERE i.BillingCountry = ‘USA’

– Group by track name
GROUP BY t.name

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

What are the three types of outer joins?

A
  1. Left Outer Join
  2. Right Outer Join
  3. Full Outer Join
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Left Outer Join

A

Returns matched rows and left table rows

Nulls will be on the right side of the table

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

Right Outer Join

A

Returns matched rows and right table rows

Nulls will be on the left side of the table

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

Full Outer Join

A

Returns all matched and unmatched rows from both tables

Nulls will be on both side of the table

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

Cartesian Product (Cross Product)

A

A cartesian product is when you have two sets of elements and you multiply them to generate all the possible pairs between the elements of the two sets.

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

SELF JOIN

A

Self joins allow you to join a table to itself.

20
Q

How do you concatenate in a select statement?

A

SELECT e.firstname || ‘ ‘ || e.lastname AS “Employee”

output = new colume with full name

21
Q

set operators

A

Take the output of two or more SELECT queries and combine them into one result.

Row oriented as they combine queries

22
Q

set operators v joins

A

both combine data.

Join clauses combine tables and are column oriented.

Set operators combine queries and are row oriented.

23
Q

4 types of set - operators

A

Union - The first is union and it returns all rows with no duplicates.

Union All - UNION ALL which returns all rows including duplicated

Intersect - returns rows outputted by both queries

Minus- returns distinct rows in the first query that are not in the second query.

24
Q

Union

A

returns all distinct rows selected by either query, effectively combing the results and duplicates

25
Union All
Return all rows selected by either query, including duplicates, without any filtering
26
Intersect
returns all distinct rows selected by both queries, effectively finding common rows
27
MINUS
Returns all distinct rows selected by the first query but not by the second query, effectively finding the difference between the two sets.
28
INTERSECT ALL
Returns all rows selected byboth queries, including duplicates, without any filtering.
29
MINUS ALL
Returns all rows selected by the first query but not the second query, including duplicates without filtering.
30
If an aliase is used in the select clasue, where can't it be used?
WHERE, GROUP BY, and HAVING Can be used in the order by
31
In a basic sql query with a SELECT FROM WHERE
32
Customising Output
uppercasing characters or selecting first characters of each cell in a column. You need functions to do this.
33
Data Type
Define what type of data a column can contain. There are three main - numeric, chacater and date.
34
Character functions Inputs and Outputs
Input: character values Outputs: character, numeric, date values Date functions/general functions/conversion functions
35
Number functions
Inputs: numeric values Outputs: numeric values
36
SUBSTRING
Character type SUBSTR(column, m, n) returns portion of a string from positions m, n how long i.e. the number of character you want to return from position m
37
LENGTH
Character function SELECT LENGTH('cat') solution would be 3 - returns length of a string.
38
REPLACE
character function SELECT REPLACE('kayak', 'k', 'y') Will replace all the the second condition of the function with the third therefore the result would be yayay
39
ROUNDING Total - 11.94 Round totals to 1 and 0
numeric function ROUND(column, m): round column to m decimal ROUND(Total, 1) = 11.9 ROUND(total, 0) = 12
40
TRUNC Total = 15.86 trunc to 1 and 0
Numeric function TRUNC(column, m): truncates column to m decimals TRUNC(Total, 1) = 15.8 TRUNC(Total, 0) = 15 Cuts of the decimal without rounding
41
Null Values deinition
No Value Not the same as 0 Arithmetic expression with NULL evaluate to NULL NULL + 10 = NULL Aggregate functions often ignore nulls such as count
42
What do you do to test for nulls?
IS NULL to check is null IS NOT NULL to check it is not null
43
NVL
Used to convert null value to non-null values. NVL(x, y): convert x, which may contain a null value, to y, a non-null value
44
NULLIF
NULLIF(x, y): Compares x and y, returns NULL if x = y x if they are not equal
45