Sqlnotes 2
Sqlnotes 2
UPDATE <table>
SET <field> = <new value>
WHERE <field> = <existing value>
//Change the value in the fields of the record that fits the condition to the new value
SELECT <field(s)>
FROM <table>
ORDER BY <field1>, … , <fieldn>
//Order the records alphabetically by the first field then the next field
SELECT <field(s)>
FROM <table>
ORDER BY <field1> DESC, … , <fieldn> DESC
//Order the records alphabetically in descending order by the first field then the next field
SELECT <field(s)>
FROM <table>
//Limit the amount of fields on the table
SELECT *
FROM <table>
//Show all fields of the table
SELECT <field(s)>
FROM <table>
WHERE <condition>// Show the records that fit the condition
SELECT <field(s)>
FROM <table>
WHERE <condition> LIKE <%pattern>
//The records where the pattern exists at the end of the value
LIKE <pattern%>
//The records where the pattern exists at the beginning of the value
LIKE <%pattern%>
//The records where the pattern exists anywhere in the value
SELECT <field(s)>
FROM <table>
WHERE <condition> AND <condition>
//The records where both the conditions are true
SELECT <field(s)>
FROM <table>
WHERE <condition> OR <condition>
//The records where either of the conditions are true
SELECT <field(s)>
FROM <table>
WHERE <field> BETWEEN <value1> AND <value2>
//The records where the field has values between the two values specified
SELECT <field(s)>
FROM <table>
WHERE <condition>
LIMIT <value>
//Only the records before the limit value are shown
SELECT <fields>
FROM <table>
WHERE <condition>
GROUP BY <field>
HAVING (<condition1>, <condition2>…)
//Having is for the conditions for the new result set
NB* use only when using GROUP BY or aggregate funtion
CDate
The function called "CDate" will convert any value to a date as long as the expression is a valid
date
Example finding the ave(time field) will give a decimal answer
Cdate(ave(time field) will give you a result in time format.
This can be used on dates as well. It does the formatting for you.
SELECT COUNT(*)
FROM <table>
WHERE <condition>
//Counts the amount of records in the table with the condition
FROM <tableName>
FROM Passengers
order by rnd(passengerId)
Select Int ((6 - 1 + 1) * Rnd + 1) would return a random number between 1 and 6
Select Int ((200 - 150 + 1) * Rnd + would return a random number between 150 and 200
150)
So just adding a random amount to the first parameter gets you a pseudo random date
To format time
Select format(date(),”HH:MM:SS”))
Format to 2 decimal
SELECT Guitars.Brand, Guitars.Series, Format(Guitars.Price,'0000.00') AS formatedPrice
FROM Guitars;
//Gives the remainder of the first value after dividing by the second value, same as the % for java
SELECT <field>
FROM <table>
WHERE <field> IN (<value>, <value> …)
SELECT *
FROM products
WHERE product_name NOT IN ('Pear', 'Banana', 'Bread');
SELECT column_name(s)
FROM table_name
WHERE column_name IN (select …);
FROM Customer
WHERE Country IN
(SELECT Country
FROM Supplier)
SELECT <table>.<field>
FROM <table1>
INNER JOIN <table2> ON <table1>.<field> = <table2>.<field>
//Inner join with full format and the order of the tables do not matter
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right
table (table2). The result is NULL in the right side when there is no match.
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left
table (table1). The result is NULL in the left side when there is no match.
FROM tblActs
Example
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers
WHERE Country='Germany';
IF field not in the table that you are selecting from you need to specify the value
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, ‘new York’ FROM Suppliers
WHERE Country='Germany';
update tblPoints
set judge3 = 6 where actnum=(select actnum from tblActs where actname="wey-wey")
Altering a table
• ALTER TABLE <table>
ADD COLUMN <newfield> <type>;
• ALTER TABLE <table>
ALTER COLUMN <oldfield> <type>;
• ALTER TABLE <table>
DROP COLUMN <oldfield>;
Example