sql_flashcards
(32 cards)
What naming convention should be used for columns and tables in a database?
Columns should be singular (e.g., ‘name’), while tables can be plural.
Where is data stored in a database server?
On the server’s hard disk.
What are the sizes and ranges of TINYINT, SMALLINT, INT, and BIGINT?
TINYINT = 1 byte → 0 to 255
SMALLINT = 2 bytes → -32,768 to 32,767
INT = 4 bytes
BIGINT = 8 bytes
What does NUMERIC(p, s) mean in SQL?
Precision (p) = total digits (max 38), Scale (s) = digits after decimal point (0 ≤ s ≤ p).
What is a database schema?
A blueprint defining tables, columns, types, relationships, views, and indexes.
What does SELECT * do in SQL?
Uses the wildcard * to select all columns.
How does DISTINCT work with two columns?
Returns unique combinations of the two columns.
What is a SQL view?
A saved query, not a saved result. Created with CREATE VIEW view_name AS …
How do you limit query results in PostgreSQL vs SQL Server?
LIMIT n in PostgreSQL; TOP(n) in SQL Server.
What’s the difference between COUNT(field_name) and COUNT(*)?
COUNT(field_name) → ignores nulls
COUNT(*) → counts all rows
What does COUNT(DISTINCT field) do?
Counts unique non-null values of the field.
What is the SQL query execution order?
FROM → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT
Why can’t aliases be used in the WHERE clause?
Because WHERE is executed before SELECT, where aliases are defined.
Where can you use column aliases in SQL?
In SELECT and ORDER BY clauses.
What are % and _ wildcards used for?
% → any number of characters
_ → exactly one character
Is the LIKE operator case-sensitive?
Yes, by default.
What is the difference between IS NULL and = NULL?
Use IS NULL or IS NOT NULL; = NULL does not work.
What is an aggregate function?
A function that summarizes multiple values into one (e.g., SUM, AVG, COUNT).
How do MIN and MAX behave with non-numeric fields?
Return alphabetical lowest/highest or earliest/latest dates.
What does ROUND(field, n) do when n is positive or negative?
Positive → rounds after the decimal
Negative → rounds before the decimal
How does integer division behave in SQL?
It returns an integer result.
What’s the difference between arithmetic and aggregate operations?
Arithmetic → row-wise
Aggregate → column-wise
What happens if you don’t alias a calculated field?
The result is shown under ?column?.
What does GROUP BY do?
Groups data to compute aggregates per group.