Professional Documents
Culture Documents
Select From
Select From
SELECT:
<TABLENAME.COLUMNNAME>
SELECT p.person_first_name FROM person p; p=alias
SELECT DISTINCT p.person_first_name FROM person p; p=alias
WHERE clause – TRUE or FALSE : SELECT p.person_first_name FROM person p WHERE
p.person_first_name = ‘Jon’ ;
BETWEEN: - acts on a column and two values: SELECT p.person_first_name FROM person p
WHERE p.contacted_number BETWEEN 1 AND 20 ;
LIKE: just for strings: SELECT p.person_first_name FROM person p WHERE
p.person_first_name LIKE ‘J%’;
IN: a column and a list of potential values (values can be numeric or string or date)
SELECT p.person_first_name FROM person p WHERE p.person_first_name IN (‘Jon’ ,
‘Shannon’, ‘Fritz’ ) ;
IS: -helps address when a value in a column might be NULL; SELECT p.person_first_name
FROM person p WHERE p.last_name IS NULL;
IS NOT: SELECT p.person_first_name FROM person p WHERE p.last_name IS NOT NULL;
ORDER BY:
-comes after the WHERE clause, but the WHERE clause isn’t required;
SET FUNCTIONS
- Some of those functions help to turn column data from Tables into computed values
- Parameter of the function – the column name; PHOTO!!!
COUNT – includes NULL values when used with *
MAX – maximum value of the column specified – does not include NULL values
MIN - does not include NULL values
AVG - does not include NULL values ! ONLY ON NUMERIC columns
SUM - does not include NULL values ! ONLY ON NUMERIC columns
** How many rows I have in the table? SELECT COUNT(*) as NR FROM person;
SELECT COUNT(DISTINCT a.adress_street) FROM address a;
GROUP BY –HAVING
JOINS
- Creating a new result set from two or more tables
1. CROSS
Cartesian Product / all the rows and columns from both tables
CROSS keyword is optional
2. INNER JOIN
ON
3. OUTER JOIN – The difference between INNER and OUTER JOIN is that OUTER JOIN is ok with
null values.
1. INSERT
INSERT INTO: Table name (only one Table at a time is allowed)
Command: INSERT INTO person (person_id, person_first_name, person_date_added)
VALUES (1, ‘John’,’2013-01-14 11:43:31’);
2. UPDATE
Without WHERE clause, will affect all rows in the Table
UPDATE…SET
3. DELETE
DELETE FROM person; - all the rows in the table person
Transactions
Syntax
START TRANSACTION;
DELETE FROM
Person;
COMMIT;
Or ROLLBACK
CONSTRAINT:
ALTER TABLE
DROP TABLE
- Removes the Table from the database
-