sql_flashcards

(32 cards)

1
Q

What naming convention should be used for columns and tables in a database?

A

Columns should be singular (e.g., ‘name’), while tables can be plural.

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

Where is data stored in a database server?

A

On the server’s hard disk.

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

What are the sizes and ranges of TINYINT, SMALLINT, INT, and BIGINT?

A

TINYINT = 1 byte → 0 to 255
SMALLINT = 2 bytes → -32,768 to 32,767
INT = 4 bytes
BIGINT = 8 bytes

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

What does NUMERIC(p, s) mean in SQL?

A

Precision (p) = total digits (max 38), Scale (s) = digits after decimal point (0 ≤ s ≤ p).

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

What is a database schema?

A

A blueprint defining tables, columns, types, relationships, views, and indexes.

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

What does SELECT * do in SQL?

A

Uses the wildcard * to select all columns.

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

How does DISTINCT work with two columns?

A

Returns unique combinations of the two columns.

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

What is a SQL view?

A

A saved query, not a saved result. Created with CREATE VIEW view_name AS …

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

How do you limit query results in PostgreSQL vs SQL Server?

A

LIMIT n in PostgreSQL; TOP(n) in SQL Server.

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

What’s the difference between COUNT(field_name) and COUNT(*)?

A

COUNT(field_name) → ignores nulls
COUNT(*) → counts all rows

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

What does COUNT(DISTINCT field) do?

A

Counts unique non-null values of the field.

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

What is the SQL query execution order?

A

FROM → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT

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

Why can’t aliases be used in the WHERE clause?

A

Because WHERE is executed before SELECT, where aliases are defined.

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

Where can you use column aliases in SQL?

A

In SELECT and ORDER BY clauses.

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

What are % and _ wildcards used for?

A

% → any number of characters
_ → exactly one character

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

Is the LIKE operator case-sensitive?

A

Yes, by default.

17
Q

What is the difference between IS NULL and = NULL?

A

Use IS NULL or IS NOT NULL; = NULL does not work.

18
Q

What is an aggregate function?

A

A function that summarizes multiple values into one (e.g., SUM, AVG, COUNT).

19
Q

How do MIN and MAX behave with non-numeric fields?

A

Return alphabetical lowest/highest or earliest/latest dates.

20
Q

What does ROUND(field, n) do when n is positive or negative?

A

Positive → rounds after the decimal
Negative → rounds before the decimal

21
Q

How does integer division behave in SQL?

A

It returns an integer result.

22
Q

What’s the difference between arithmetic and aggregate operations?

A

Arithmetic → row-wise
Aggregate → column-wise

23
Q

What happens if you don’t alias a calculated field?

A

The result is shown under ?column?.

24
Q

What does GROUP BY do?

A

Groups data to compute aggregates per group.

25
Why can’t aliases be used in HAVING?
Because HAVING comes before SELECT where aliases are defined.
26
When should you use USING(field) in a JOIN?
When the field name is the same in both tables.
27
What is a one-to-one relationship in a database?
Each row in Table A corresponds to exactly one row in Table B using the same primary key.
28
How do you store images in a SQL database?
Use a binary data type (e.g., BLOB).
29
What is a many-to-many relationship?
When multiple records in one table relate to multiple in another (e.g., countries ↔ languages).
30
What does SQL style guide recommend for formatting?
Follow Holywell’s style guide: https://www.sqlstyle.guide/
31
Why should SQL queries end with a semicolon?
It terminates the query, especially needed when executing multiple statements.
32
When should you use double quotes in field names?
When the field name contains spaces or special characters (e.g., "release year").