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;