SQL Statements
There are 3 basic categories of SQL Statements:
SQL-Data Statements -- query and modify tables and columns
SELECT Statement -- query tables and views in the database
INSERT Statement -- add rows to tables
UPDATE Statement -- modify columns in table rows
DELETE Statement -- remove rows from tables
SELECT Clause
The SELECT clause is mandatory. It specifies a list of columns to be
retrieved from the tables in the FROM clause. It has the following
general format:
FROM Clause
The FROM clause always follows the SELECT clause. It lists the tables
accessed by the query.
WHERE Clause
TRUNCATE - remove all records from a table, including all spaces allocated
for the records are removed
DELETE - deletes all records from a table, the space for the records remain
The DELETE command is used to remove some or all rows from a table. A
WHERE clause can be used to only remove some rows. If no WHERE
condition is specified, all rows will be removed. After performing a DELETE
operation you need to COMMIT or ROLLBACK the transaction to make the
change permanent or to undo it. Note that this operation will cause all
DELETE triggers on the table to fire.
TRUNCATE
TRUNCATE removes all rows from a table. The operation
cannot be rolled back and no triggers will be fired. As such,
TRUNCATE is faster and doesn't use as much undo space as
a DELETE.
DROP
The DROP command removes a table from the database.
All the tables' rows, indexes and privileges will also be
removed. No DML triggers will be fired. The operation
cannot be rolled back.
DROP and TRUNCATE are DDL commands, whereas DELETE
is a DML command. Therefore DELETE operations can be
rolled back (undone), while DROP and TRUNCATE
operations cannot be rolled back.
ORDER BY Clause
INNER JOIN: This will only return rows when there is at least
one row in both tables that match the join condition.
LEFT OUTER JOIN (or LEFT JOIN): This will return rows that
have data in the left table (left of the JOIN keyword), even if
there's no matching rows in the right table.
RIGHT OUTER JOIN (or RIGHT JOIN): This will return rows
that have data in the right table (right of the JOIN keyword),
even if there's no matching rows in the left table.
FULL OUTER JOIN (or FULL JOIN): This will return all rows, as
long as there's matching data in one of the tables.
Join Syntax
Inner Join:
id name id name
1 Pirate 2 Pirate
3 Ninja 4 Ninja
Full outer join produces the set of all records in Table A and Table B, with
matching records from both sides where available. If there is no match, the
missing side will contain null.
id name id name
1 Pirate 2 Pirate
2 Monkey null null
3 Ninja 4 Ninja
4 Spaghetti null null
null null 1 Rutabaga
null null 3 Darth Vader
Left outer join produces a complete set of records from Table A, with
the matching records (where available) in Table B. If there is no
match, the right side will contain null.
id name id name
1 Pirate 2 Pirate
2 Monkey null null
3 Ninja 4 Ninja
4 Spaghetti null null
To produce the set of records only in Table A, but not in Table B, we
perform the same left outer join, then exclude the records we
don't want from the right side via a where clause.
id name id name
2 Monkey null null
4 Spaghetti null null
To produce the set of records unique to Table A and Table B, we
perform the same full outer join, then exclude the records we
don't want from both sides via a where clause.
id name id name
2 Monkey null null
4 Spaghetti null null
null null 1 Rutabaga
null null 3 Darth Vader