Basics Flashcards
(41 cards)
SQL
- standard language for storing, manipulating and retrieving data in databases.
- Structured Query Language
What can SQL do?
- EXECUTE query
- READ data
- INSERT records
- UPDATE records
- DELETE records
- create DATABASES, TABLES, STORED PROCEDURES, VIEWS
- set PERMISSIONS on tables, stored procedures and views
RDBMS
- Relational Database Management System
* stores tables consisting of columns and rows
SQL statement/syntax
- executable block of sql code
- keywords are not case sensitive
- semicolon at the end of each statement
SELECT
- reads data from a database
- returns result in a table (result-set)
- example:
SELECT * FROM table_name; -> returns all columns from table
SELECT column1, column2 FROM table_name; -> returns specified column(s) from table
SELECT DISTINCT
- reads distinct/unique values from table
- example:
SELECT DISTINCT column1 FROM table_name; -> returns unique values for column1 from table
WHERE
- filter records by given conditions
- can be use for UPDATE and DELETE statements
- example:
SELECT column1 FROM table_name WHERE condition; -> returns column1 values meeting the given condition
WHERE clause operators
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
BETWEEN Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column
AND OR NOT operators
- AND and OR are used in WHERE clause to filter for additional conditions
- NOT is used for returning opposite or not true conditions
- example:
SELECT column1 FROM table_name
WHERE condition1 AND condition2…. ; -> return values where all conditions are metSELECT column1 FROM table_name WHERE condition1 OR condition2.... ; -> return values where any conditions are met SELECT column1 FROM table_name WHERE NOT condition1; -> return values where condition is not met SELECT column1 FROM table_name WHERE condition1 AND (condition2 OR condition3) .... ; -> nested conditions SELECT column1 FROM table_name WHERE NOT condition1 AND (NOT condition2 OR condition3).... ; -> nested condition with not operator
ORDER BY
- sorts results in ascending (default) or descending order
- use keyword ASC for asecnding DESC for descending
- example:
SELECT column1, column2 FROM table_name
ORDER BY column1, column2, … ASC(DESC); -> sorts results in ASCending order (or DESCending order)
INSERT INTO
- example:
INSERT INTO table_name (column1, column2, …)
VALUES (value1, value2, …); -> values 1 goes to column1, value2 to column2, etcINSERT INTO table_name VALUES (value1, value2, ...); -> values 1 goes to column1, value2 to column2, and so on as per columns order on table
NULL
- field with no value (saved as NULL)
- use IS NULL or IS NOT NULL to test for null values
- example:
SELECT column1 FROM table_name
WHERE condition1 IS NULL; -> return rows with null valuesSELECT column1 FROM table_name WHERE condition1 IS NOT NULL; -> return rows without null values
UPDATE
- use to modify existing records in a table
- WHERE clause is used to specify which record to update, if no where clause then all records are updated
- where clause can update more than 1 record depending on filter result
- example:
UPDATE table_name
SET column1 = value1, column2 = value2, …
WHERE condition(s); -> modify column values filtered by condition
DELETE
- remove records from table
- WHERE clause to filter record(s) to remove
- if no where clause then all records are removed
- example:
DELETE FROM table_name
WHERE condition(s); -> remove record(s) filtered by conditionDELETE FROM table_name; -> remove all records
SELECT number of rows
- same as select but limit number of records returned
- uses different syntax depending on db: LIMIT, ROWNUM, TOP
- example:
SELECT TOP number column_name(s)
FROM table_name
WHERE condition; -> return ‘number’ of rows, used by SQL and MS AccessSELECT TOP number PERCENT column_name(s) FROM table_name WHERE condition; -> return percentage of rows, used by SQL and MS Access
SELECT column_name(s) FROM table_name WHERE condition LIMIT number; -> return 'number' of rows, used by MySQL
SELECT TOP number column_name(s) FROM table_name WHERE ROWNUM <= number; -> return 'number' of rows, used by Oracle
aggregate functions
AVG MIN MAX SUM COUNT
- returns a value based on function called
- most popular functions are: MIN(), MAX(), AVG(), COUNT(), SUM()
- NULL values are ignored
- example:
SELECT MIN(column_name) as a_different_name
FROM table_name
WHERE condition; -> returns minimum value
LIKE
- used in WHERE clause to search to specific patterns
- uses 2 types of wildcards: % -> to represent 0 to infinite char, _ -> represent single char
- use of LIKE:
WHERE column LIKE ‘a%’ -> Finds any values that start with “a”
WHERE column LIKE ‘%a’ -> Finds any values that end with “a”
WHERE column LIKE ‘%or%’ -> Finds any values that have “or” in any position
WHERE column LIKE ‘r%’ -> Finds any values that have “r” in the second position
WHERE column LIKE ‘a%’ -> Finds any values that start with “a” and are at least 2 characters in length
WHERE column LIKE ‘a__%’ -> Finds any values that start with “a” and are at least 3 characters in length
WHERE column LIKE ‘a%o’ -> Finds any values that start with “a” and ends with “o” - example:
SELECT column_name(s) FROM table_name
WHERE column LIKE pattern;
IN
- substitute for OR conditions
- allows multiple values in where clause
- example:
SELECT column_name(s) FROM table_name
WHERE column IN (value1, value2, …); -> values can also come from another select statement
BETWEEN
- use for range condition between two values
- example:
SELECT column_name(s) FROM table_name
WHERE column BETWEEN value1 AND value2;
Aliases
- casting table or column as another name
- used to make column/table names more readable
- only work during execution of query
- example:
SELECT column_name AS alias_name
FROM table_name AS alias_name;SELECT o.OrderID, o.OrderDate, c.CustomerName FROM Customers AS c, Orders AS o WHERE c.CustomerName='Around the Horn' AND c.CustomerID=o.CustomerID;
JOINs
- combine rows from 2+ tables
- combination is through a common column between tables
- INNER JOIN - returns matching values in both tables:
SELECT table1.column1, table2.column1, …
FROM table1
INNER JOIN table2 ON table1.column_name=table2.column_name; - LEFT JOIN - return all result from left table (table1) puls matching results from right table (table2) or NULL if right table has no match:
SELECT table1.column1, table2.column1, …
FROM table1
LEFT JOIN table2 ON table1.column_name=table2.column_name; - RIGHT JOIN - return all result from right table (table2) puls matching results from left table (table1) or NULL if left table has no match:
SELECT table1.column1, table2.column1, …
FROM table1
RIGHT JOIN table2 ON table1.column_name=table2.column_name;
*FULL JOIN - returns values when there is a match in either tables, in other words, return all records in both tables:
SELECT table1.column1, table2.column1, …
FROM table1
FULL JOIN table2 ON table1.column_name=table2.column_name;
*example:
SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);
UNION
- UNION - combines 2 SELECT statements, basically combining two tables
- they must have same schema: - similar datatypes, # of columns and column order
- UNION only select DISTINCT values, UNION ALL takes duplicates
- example:
SELECT column(s) FROM table1
UNION (ALL - for duplicates)
SELECT column(s) FROM table2;
GROUP BY
- group rows with similar values into a summary
- uses aggregate functions (COUNT MIN MAX SUM AVG) to group result-sets by column(s)
- example:
SELECT AVG(column), column1
FROM table_name
GROUP BY column1;SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country ORDER BY COUNT(CustomerID) DESC;
HAVING
- WHERE clause for GROUP BY
- allows you to have condition/filter on the aggregate function after grouping
- example:
SELECT AVG(column), column1
FROM table_name
GROUP BY column1
HAVING AVG(column) condition;
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5;