SQL Flashcards
relational database
A relational database is a database that organizes information into one or more tables.
SQL statement
SQL statement is text that the database recognizes as a valid command. Statements always end in a semi-colon ;.
let’s create a new table in SQL
CREATE TABLE celebs (id INTEGER, name TEXT, age INTEGER); CREATE TABLE is a clause that tells SQL you want to create a new table. 2. celebs is the name of the table. 3. (id INTEGER, name TEXT, age INTEGER) is a list of parameters defining each column in the table and its data type. id is the first column in the table. It stores values of data type INTEGER name is the second column in the table. It stores values of data type TEXT age is the third column in the table. It stores values of data type INTEGER
Add a row to the table.
INSERT INTO celebs (id, name, age) VALUES (1, ‘Justin Bieber’, 21);
How to query only interested columns.
SELECT is used every time you want to query data from a database. SELECT column1, column2 FROM table_name;
SELECT * FROM celebs;
You can also query data from all columns in a table with SELECT. SELECT statements always return a new table called the result set.
let’s edit a row
UPDATE celebs SET age = 25 WHERE id = 1;
Add a new column to the table.
ALTER TABLE celebs ADD COLUMN twitter_handle TEXT;
Delete all of the rows that have a NULL value in the twitter column.
DELETE FROM celebs WHERE twitter_handle IS NULL;
Create a new table with constraints on the values
CREATE TABLE awards ( id INTEGER PRIMARY KEY, recipient TEXT NOT NULL, award_name TEXT DEFAULT “Grammy”);
Constraints that add information about how a column can be used are invoked after specifying the data type for a column. They can be used to tell the database to reject inserted data that does not adhere to a certain restriction. examples?
- PRIMARY KEY columns can be used to uniquely identify the row. Attempts to insert a row with an identical value to a row already in the table will result in a constraint violation which will not allow you to insert the new row. 2. UNIQUE columns have a different value for every row. This is similar to PRIMARY KEY except a table can have many different UNIQUE columns. 3. NOT NULL columns must have a value. Attempts to insert a row without a value for a NOT NULL column will result in a constraint violation and the new row will not be inserted. 4. DEFAULT columns take an additional argument that will be the assumed value for an inserted row if the new row does not specify a value for that column.
WHATS SQL
SQL is a programming language designed to manipulate and manage data stored in relational databases.One of the core purposes of the SQL language is to retrieve information stored in a database. This is commonly referred to as querying. A relational database is a database that organizes information into one or more tables. A table is a collection of data organized into rows and columns.
how to renamed the “name” column to Movies in table “movies”
SELECT name AS ‘Movies’ FROM movies; AS is a keyword in SQL that allows you to rename a column or table using an alias. The new name can be anything you want as long as you put it inside of single quotes. It is important to remember that the columns have not been renamed in the table. The aliases only appear in the result.
DISTINCT statement
DISTINCT is used to return unique values in the output. It filters out all duplicate values in the specified column(s).e.g. SELECT DISTINCT tools FROM inventory;
WHERE clause
We can restrict our query results using the WHERE clause in order to obtain only the information we want. Following this format, the statement below filters the result set to only include top rated movies (IMDb ratings greater than 8): SELECT * FROM movies WHERE imdb_rating > 8;
what is is an operator
Operators create a condition that can be evaluated as either true or false. Comparison operators used with the WHERE clause are: = equal to != not equal to > greater than < less than
LIKE statement
LIKE is a special operator used with the WHERE clause to search for a specific pattern in a column. e.g. The movies table contains two films with similar titles, ‘Se7en’ and ‘Seven’. SELECT * FROM movies WHERE name LIKE ‘Se_en’; here, name LIKE ‘Se_en’ is a condition evaluating the name column for a specific pattern. Se_en represents a pattern with a wildcard character. The _ means you can substitute any individual character here without breaking the pattern. The names Seven and Se7en both match this pattern.
wildcard character % and _
% is a wildcard character that matches zero or more missing letters in the pattern. This statement below filters the result set to only include movies with names that begin with the letter ‘A’: SELECT * FROM movies WHERE name LIKE ‘A%’; another e.g. , any movie that contains the word ‘man’ in its name will be returned in the result below. SELECT * FROM movies WHERE name LIKE ‘%man%’; LIKE is not case sensitive. ‘Batman’ and ‘Man of Steel’ will both appear in the result of the query above.
The _ means you can substitute any individual character here without breaking the pattern.
To filter for all movies with an IMDb rating in the name column of table movies
SELECT name FROM movies WHERE imdb_rating IS NOT NULL;
BETWEEN operator
BETWEEN operator can be used in a WHERE clause to filter the result set within a certain range. The values can be numbers, text or dates. This statement filters the result set to only include movies with names that begin with letters ‘A’ up to but not including ‘J’. SELECT * FROM movies WHERE name BETWEEN ‘A’ AND ‘J’; In this statement, the BETWEEN operator is being used to filter the result set to only include movies with years between 1990 up to and including 1999. SELECT * FROM movies WHERE year BETWEEN 1990 AND 1999; Really interesting point to emphasize again: BETWEEN two letters is not inclusive. BETWEEN two numbers is inclusive.
how to combine multiple conditions in a WHERE clause to make the result set more specific and useful.
e.g. we use the AND operator to only return 90’s romance movies. SELECT * FROM movies WHERE year BETWEEN 1990 AND 1999 AND genre = ‘romance’;
Suppose we want to check out a new movie or something action-packed:
SELECT * FROM movies WHERE year > 2014 OR genre = ‘action’;
ORDER BY
ORDER BY always goes after WHERE! ORDER BY is a clause that indicates you want to sort the result set by a particular column. if we want to sort everything by the movie’s title from A through Z SELECT * FROM movies ORDER BY name; Sometimes we want to sort things in a decreasing order. For example, if we want to select all of the well-received movies, sorted from highest to lowest by their year: SELECT * FROM movies WHERE imdb_rating > 8 ORDER BY year DESC; DESC is a keyword used in ORDER BY to sort the results in descending order (high to low or Z-A). ASC is a keyword used in ORDER BY to sort the results in ascending order (low to high or A-Z). The column that we ORDER BY doesn’t even have to be one of the columns that we’re displaying.
CASE statement
It is SQL’s way of handling if/then logic. A CASE statement allows us to create different outputs (usually in the SELECT statement). SELECT name, CASE WHEN imdb_rating > 7 THEN ‘Good’ WHEN imdb_rating > 5 THEN ‘Okay’ END FROM movies; Each WHEN tests a condition. The following THEN gives us the string if the condition is true. CASE statement must end with END. Here, we set all movies above 8 as ‘Good’ and movies above 5 ‘Okay’.