DATABASE Flashcards
WHAT IS A ROW IN A DATABASE?
record
WHAT IS A COLUMN IN A DATABASE?
field
SELECT *
FROM country;
get all columns from country table
SELECT id, name
FROM city;
fetches id and name columns from city table
SELECT name
FROM city
ORDER BY rating (ASC);
selects name column from city table and sorts it by rating in ascending order
SELECT name
FROM city
ORDER BY rating (DESC);
selects name column from city table and sorts it by rating in descending order
SELECT name
FROM city
WHERE rating > 3;
fetch names from city table where the rating is above 3
SELECT name
FROM city
WHERE name != ‘Berlin’
AND name != ‘Madrid’;
fetch names from city table that are neither Berlin or Madrid
SELECT name
FROM city
WHERE name LIKE ‘P%’
OR name LIKE ‘%s’ ;
fetch names of cities where their names start with P or ends in S
SELECT name
FROM city
WHERE name LIKE ‘_ublin’;
fetch names of cities with any letter followed by ‘ublin’
SELECT name
FROM city
WHERE population BETWEEN 50000 AND 5000000;
select names of cities where population is between 50K and 5M
SELECT name
FROM city
WHERE rating IS NOT NULL;
fetch names of cities where their rating does not miss a value
SELECT name
FROM city
WHERE country_id IN (1, 4, 7, 8);
fetch names of cities that are in countries with IDs 1, 4, 7, and 8
WHAT DOES “INNER JOIN” do?
SELECT city.name, country.name
FROM city
[INNER] JOIN country
ON city.country_id = country.id;
returns rows that have matching values in both tables
ex.
SELECT city.name, country.name
FROM city
INNER JOIN country
ON city.country_id = country.id;
WHAT DOES “LEFT JOIN” DO?
SELECT city.name, country.name
FROM city
LEFT JOIN country
ON city.country_id = country.id;
returns all rows from the left table with corresponding rows from the right table
if there’s no matching row, NULLs are returned as values from the second table
WHAT DOES “RIGHT JOIN” DO?
SELECT city.name, country.name
FROM city
RIGHT JOIN country
ON city.country_id = country.id;
returns all rows from the right table with corresponding rows from the left table
if there’s no matching row, NULLs are returned as values from the left table
WHAT DOES “FULL JOIN” DO?
SELECT city.name, country.name
FROM city
FULL JOIN country
ON city.country_id = country.id;
returns all rows from both tables
WHAT DOES “CROSS JOIN” do?
SELECT city.name, country.name
FROM city
CROSS JOIN country;
SELECT city.name, country.name
FROM city, country;
returns all possible combinations of rows from both tables
WHAT DOES “NATURAL JOIN” DO?
SELECT city.name, country.name
FROM city
NATURAL JOIN country;
joins tables by all columns with the same name
WHAT DOES “ALTER TABLE” DO?
ALTER TABLE table_name
ADD column_name datatype;
allows you to add columns to a table in database
WHAT DOES “AS” DO?
SELECT column_name AS ‘ALIAS’
FROM table_name;
allows you to rename a column or table using an alias