SQL Basics Flashcards
What is a relational database?
A collection of tables where each table is related to one another in some way
What is a table comprised of?
Columns and rows/records
T/F: The primary key
column can contain duplicate data
False
T/F: The primary key
column can contain null values
False
Insert ‘Baby’ and ‘Domestic’ into a table called ‘departments’ which has two columns
insert into departments values ('Baby', 'Domestic');
Create a table called ‘departments’ with two columns, ‘department’ and ‘division’, which have type ‘varchar(100)’. Make the primary key ‘department’.
create table departments ( department varchar(100), division varchar(100), primary key (department) );
T/F: You can have more than one primary key constraint in a table
False
T/F: You can have multiple columns acting as one primary key
True
What does ; do in a query?
It separates statements in one script
Write a WHERE clause that returns all ‘department’ values that start with ‘F’ and end with ‘e’
WHERE department like ‘F%e’;
Write a WHERE clause that returns all ‘salary’ values greater than 10
WHERE salary > 10;
Write a WHERE clause that returns values where ‘salary’ < 40000 and ‘department’ is either ‘Clothing’ or ‘Pharmacy’
WHERE 'salary' < 40000 AND (department = 'Pharmacy' OR department = 'Clothing');
Write a WHERE clause where ‘department’ does not equal ‘Sports’
WHERE NOT department = ‘Sports’;
What’s another way of writing negative assertion other than NOT?
!= or <>
Write a WHERE clause that returns all emails that are NULL
WHERE email IS NULL;
Write a WHERE clause that returns all emails that are not NULL
WHERE email IS NOT NULL;
Write a WHERE clause that returns department values that equal ‘Sports’, ‘First Aid’, ‘Toys’, and ‘Garden’
WHERE department IN (‘Sports’, ‘First Aid’, ‘Toys’, ‘Garden’);
T/F: BETWEEN 80 AND 100
is inclusive
True
Write a WHERE clause that returns ‘salary’ between 80 and 100
WHERE salary BETWEEN 80 AND 100;
How do you write comments in an SQL query?
Use – to comment out a line
Write a clause that orders the result by employee_id in descending order
ORDER BY employee_id DESC
Write a query that returns all the distinct ‘department’ values from the ‘employees’ table
SELECT DISTINCT department FROM employees
What does ORDER BY 1 mean?
It sorts in ascending order based on the first column in the table
Limit the returned results by 10 using two different methods
LIMIT 10
FETCH FIRST 10 ROWS ONLY