SQL DAY 2
SQL Not
SQL Insert Into
SQL Null Values
SQL Update
SQL Delete
SQL Select Top
SQL Aggregate Functions
SQL Min and Max
SQL Count
SQL Sum
SQL Avg
SQL Like
NOT
The NOT operator is used in combination with other operators to give the opposite result,
also called the negative result.
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
INSERT INTO
To insert new records into table
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
(Browser does not support WebSQL)
NULL Values
A field with a NULL value is a field with no value.
If a field in a table is optional, it is possible to insert a new record or update a record without
adding a value to this field. Then, the field will be saved with a NULL value.
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
IS NULL
NOT NULL
UPDATE
The UPDATE statement is used to modify the existing records in a table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Note: Be careful when updating records in a table! Notice the WHERE clause in the
UPDATE statement. The WHERE clause specifies which record(s) that should be updated. If
you omit the WHERE clause, all records in the table will be updated!
(Browser does not support WebSQL)
DELETE
The DELETE statement is used to delete existing records in a table.
DELETE FROM table_name WHERE condition;
Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE
statement. The WHERE clause specifies which record(s) should be deleted. If you omit the
WHERE clause, all records in the table will be deleted!
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
DELETE FROM Customers; Deletes all records
DROP TABLE Customers; Deletes the table completely
SELECT TOP
The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause is useful on large tables with thousands of records. Returning a
large number of records can impact performance.
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
AGGREGATE FUNCTIONS
An aggregate function is a function that performs a calculation on a set of values, and returns
a single value.
Aggregate functions are often used with the GROUP BY clause of the SELECT statement.
The GROUP BY clause splits the result-set into groups of values and the aggregate function
can be used to return a single value for each group.
MIN() - returns the smallest value within the selected column
MAX() - returns the largest value within the selected column
COUNT() - returns the number of rows in a set
SUM() - returns the total sum of a numerical column
AVG() - returns the average value of a numerical column
LIKE
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
The percent sign % represents zero, one, or multiple characters
The underscore sign _ represents one, single character
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Thara