SQL Fundamentals · SQL Foundations
19 cards
SQL Lexical Structure
-
Quick check
A script holds several statements in one file. What normally marks the end of each command?
AA comma between the statements
A comma separates items inside a statement, such as entries of a select list, and never closes the command.
BA double dash before the next statement
A double dash opens a comment that runs to the end of the line; it terminates nothing.
CA semicolon, or the end of the input stream
Right. A command is a sequence of tokens terminated by a semicolon or by the end of the input stream.
2 / 19
-
Quick check
How does PostgreSQL treat a comment before syntax analysis?
AIt removes the comment, which then acts like whitespace
Right. Comments are not tokens: they are removed and effectively become whitespace before the syntax is analysed.
BIt evaluates the comment as a string constant
A comment is never evaluated; only a quoted constant produces a value.
CIt keeps the comment as a token so the parser can skip it
Comments are explicitly not tokens, so nothing keeps them for the parser to skip over.
4 / 19
-
Quick check
A column is created unquoted as SalesTotal. Which statement about that name is true?
AIts capital letters are preserved, because names are stored as typed
Nothing preserves the capitals of an unquoted name; PostgreSQL does not store it as typed.
BIt is folded to lower case, because unquoted names are case-insensitive
Right. Unquoted identifiers are case-insensitive and PostgreSQL folds them to lower case.
CIt becomes a keyword, because it was written without any quoting
Keywords have fixed meanings of their own; writing a name unquoted never turns it into one.
6 / 19
-
Keep your progress in the app
That’s 3 of 9 quick checks. In the app they stay answered, and every lesson remembers where you left off.
-
Quick check
A query must refer to a column named select and another named SalesTotal, keeping both names exactly. Which select list works?
A"select", "SalesTotal"
Right. Double quotes make select an identifier rather than a keyword and preserve the mixed case of SalesTotal.
B'select', 'SalesTotal' in single quotes
Single quotes produce string constants, so the query would select two pieces of text instead of two columns.
Cselect, SalesTotal
Unquoted, select collides with the keyword and SalesTotal is folded to lower case, so neither name survives.
8 / 19
-
Quick check
A value must contain the text Dianne's horse using the regular SQL string form, with no PostgreSQL-specific extension. Which expression fits?
AE'Dianne\'s horse', which escapes the apostrophe with a backslash
An escape string is a PostgreSQL extension, and its backslash escapes are recognized only in that form.
B$$Dianne's horse$$, which preserves its content literally
Dollar quoting keeps the content literally but is not part of the SQL standard, so it fails the second requirement.
C'Dianne''s horse'
Right. The regular form uses single-quote delimiters and writes the embedded apostrophe as two adjacent single quotes.
11 / 19
-
Quick check
A statement contains the whole-number constant 42, which fits comfortably in integer. What type does it start with?
Anumeric, the type used for every literal number
numeric is the initial type of a constant written with a decimal point or an exponent, and the fallback when neither integer type fits.
Binteger, the narrowest size that fits it
Right. A whole-number constant starts as integer when it fits, then bigint, and otherwise numeric.
Cbigint, because whole numbers start at the widest size
The sizes are tried from the narrowest that fits, so bigint is reached only when integer is too small.
13 / 19
-
Quick check
A Boolean argument and a date literal must both be written so their types are unmistakable. Which pair does that?
Atrue, and DATE '2030-01-15'
Right. Boolean values are supplied as the bare tokens true and false, and prefixing the string with DATE states the intended type instead of leaving it to context.
B'true', and the plain string '2030-01-15'
Quoting true makes it a string constant, and a bare quoted date leaves its type to contextual inference.
C"true", and 2030-01-15
Double quotes make true an identifier, and an unquoted date is not a string constant at all.
15 / 19
-
Quick check
An expression must state an exact numeric target type using the SQL-conforming conversion syntax rather than a PostgreSQL shorthand. Which form fits?
A1.23, relying on the initial type the constant is given
A bare constant leans on its initial typing and on context, so no target type is stated at all.
BCAST (1.23 AS numeric)
Right. CAST states the numeric target explicitly and is the conversion form that conforms to SQL.
C1.23::numeric, which names the target after a double colon
The double-colon form does request numeric, but it is historical PostgreSQL syntax rather than the SQL-conforming one.
17 / 19
-
Quick check
Which summary of SQL's lexical rules is right?
AA comma ends a command, and single quotes delimit the names of database objects
A comma separates items inside a statement, and single quotes delimit string constants rather than object names.
BA semicolon ends a command, double quotes delimit identifiers, and a doubled single quote is an apostrophe inside a regular string
Right. Those are the three habits that keep commands, names, and text apart from one another.
CA double dash ends a command, and an unquoted name keeps whatever capitals it was written with
A double dash opens a comment to the end of the line, and an unquoted name is folded to lower case.
19 / 19
-
9 quick checks · then the test
In the app, finishing the quick checks opens this lesson’s 10-question test, and the ones you miss come back exactly when you’re about to forget them.
The whole course, on your phone
Lessons you can read, audio you can listen to on the way to work, and practice that remembers what you got wrong.