SQL Syntax Flashcards

1
Q

What is the structure of a basic SQL query?
And what do they refer to?

A

A basic SQL query contains 3 clauses…

SELECT - columns you want to use/see
FROM - table you want to reference
WHERE - criteria you want to use to filter rows (using operators)

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

How would you return ALL columns?

A

SELECT *

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

What special character do you use at the end of a SQL statement and what is it’s purpose/name?

A

; (terminator)

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

What is a query?

A

A request for a certain set of data/information from a database

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

What is the FROM syntax?

A

Dataset.TableName

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

What is the SELECT syntax for multiple columns

A

Column names with comma separator (except for final column)

SELECT
columnA,
columnB,
columnC

Quirky: DON’T use comma separators for multiple WHERE criteria

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

How do you rename ugly/long column names to be more readable? What do you call this? How do you include spaces in the name?

A

Use a column alias.
For spaces, must a b or [a b]

SELECT 
     CustomerID AS NewID,
     Badname AS `Better Name`,
     Crapname AS [New Name]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is SQL case-sensitive?

A

No, but using caps and indentation can make it more readable.
Column names - use_snake_case
Table names - Camel_Case

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

What is the comment syntax?

A

– comment (most common)
or
/* comment */

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

WHERE: What is syntax for an exact match? Text? Number?

A

WHERE
CustomerID = 149;

WHERE
CustomerName = ‘Bob’

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

WHERE: How do you search using Starts with? Ends with? Contains? What is the special character called?

A

Wildcard character % used for searching text strings. Almost always used with LIKE operator (some databases use * instead)

WHERE Name LIKE ‘A%’
WHERE Name LIKE ‘%a’
WHERE Name LIKE ‘%a%’

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

WHERE: How would you search for records where the third character of first name is a?

A

WHERE Firstname LIKE ‘__a%’

Use two underscore characters.

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

How do you find records with a blank value in columnA? or all non-blank records?

A
WHERE columnA IS NULL
WHERE columnA IS NOT NULL
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you filter records in a SQL query?

A

WHERE clause is where all filtering criteria go (uses boolean logic)

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

How do you sort records (ascending or descending) that are being output from a SQL statement? How would you sort with multiple criteria?

A

ORDER BY clause (at end of statement with ; terminator after). Default sort order is ascending (ASC) but can be changed to descending with DESC… separate multiple criteria with comma separtor

ORDER BY
Column1Name DESC,
Column2Name ASC;

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

What’s ORDER BY for and where is it placed in a SQL statement?

A

ORDER BY is for sorting rows based on a particular column value. It belongs after FROM and WHERE - if used, but before LIMIT. Default is ascending, but can use DESC for descending…

WHERE ColumnName = ‘value’
ORDER BY ColumnName DESC
LIMIT 1000

17
Q

How do you restrict the number of rows to be returned and when might you need to do this?

A

LIMIT the number of rows returned (placed at very end of query - after any WHERE and ORDER BY)

SELECT * FROM dataset.table
WHERE ColumnName < 50
ORDER BY ColumnName
LIMIT 1000

Really large datasets (save query time), preview results

18
Q

Why might you want to create a new table? How do you do it?

A

You may want to create a table if the original dataset is changing and you want to capture the data at a point in time OR if you know you just want to work on specific subsets of a larger dataset for simplicity (rather than one really complex query that is continually updated for different problems, you can create a new table - and conduct queries on that new table). Save results to New Table or change query settings to output to New Table