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