Basics Flashcards
1
Q
SQL
A
- standard language for storing, manipulating and retrieving data in databases.
- Structured Query Language
2
Q
What can SQL do?
A
- EXECUTE query
- READ data
- INSERT records
- UPDATE records
- DELETE records
- create DATABASES, TABLES, STORED PROCEDURES, VIEWS
- set PERMISSIONS on tables, stored procedures and views
3
Q
RDBMS
A
- Relational Database Management System
* stores tables consisting of columns and rows
4
Q
SQL statement/syntax
A
- executable block of sql code
- keywords are not case sensitive
- semicolon at the end of each statement
5
Q
SELECT
A
- 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
6
Q
SELECT DISTINCT
A
- reads distinct/unique values from table
- example:
SELECT DISTINCT column1 FROM table_name; -> returns unique values for column1 from table
7
Q
WHERE
A
- 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
8
Q
WHERE clause operators
A
= 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
9
Q
AND OR NOT operators
A
- 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
10
Q
ORDER BY
A
- 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)
11
Q
INSERT INTO
A
- 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
12
Q
NULL
A
- 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
13
Q
UPDATE
A
- 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
14
Q
DELETE
A
- 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
15
Q
SELECT number of rows
A
- 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
16
Q
aggregate functions
AVG MIN MAX SUM COUNT
A
- 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