SQL
Structured Query Language
Example: CD_Database
- Tabl :Owner
- Tabl: CD
Select Statement
- SELECT ( what fields you want to display)
- * = all fields
- FROM (table contains the fields)
- WHERE( list conditions or “filters” that must be applied to the data)
SELECT Eg:
SELECT* FROM CD WHERE Artist > “M”
SELECT* FROM CD WHERE Genre = “Pop” AND ReplacementValue > 100
SELECT* FROM CD WHERE Genre = “Pop” OR ReplacementValue > 100
SELECT* FROM CD WHERE (Genre = “Pop” OR Genre = “Rock”) AND ReplacementValue >
100
(REMEMBER ALPHABETICAL ORDER)
SELECT * FROM CD WHERE NOT Genre = “Rock”
SELECT * FROM CD WHERE NOT Genre is NULL
SELECT * FROM CD WHERE NOT Genre IS NOT NULL
…WHERE ReplacementValue BETWEEN 100 AND 200
…WHERE Artist BETWEEN “B” AND “F”
…WHERE Genre IN (“Rock”, “POP”,”Jazz”)
LIKE
● * = none or all character of any length
● …WHERE Artists LIKE “S”
● …WHERE Artists LIKE “S*” // name begins with S
● … WHERE Artist “*S” // last letter is S
● …WHERE Artist “*S*” // has S somewhere in the name
- ? = exactly ONE character
- WHERE Artists LIKE “????”// 4 characters in their name
- WHERE Artists LIKE “M???” // 4 Characters but starting with M
* and ? delphi don't recognize
- Use % instead of *
- Use _ instead of ?
-
SQL TIPS
1.
Smelly
Feet
Will
Give
Horrible
Odours
SELECT (Field Names)
FROM ( Table name)
WHERE (Criteria)
GROUP BY (field names)
HAVING (criteria)
ORDER BY (field) AC/DESC
2. Multiple Tables
SELECT <table name>surname, mark,<other table name> surname…
FROM tblCD, tblOwner
WHERE tblCD.OwnerID = tblOwner.OwnerID
AND ReplaementValue > 50…
3. Aggregate and Grouping
SELECT Grade, AVG(Price)
FROM tblData
WHERE Grade >= 10
GROUP BY Grade
HAVING AVG(Price) >= 100
4. Variables
iInput := spnMark.Value;
‘...WHERE Mark>= ‘+iInput+’ AND – DELPHI
‘...WHERE surname = “‘+sInput+’” …’
THIS IS ONLY FOR LIKE
- Use % instead of *
- Use _ instead of ?
‘SELECT Title,Surname, TeacherCode FROM EducatorsTb WHERE Subjects = “ENG” OR
Subjects = “AFR” OR Subjects = “XHO”
// calculate aggregate
‘SELECT Count(s5) AS [IT NUMBERS] FROM LearnersTb WHERE s5 = “IT”
//
‘SELECT Name, Surname, FROM LearnersTb WHERE Teachers LIKE “%MB%”’;
// AGGREGATE
‘SELECT RegisterTeacher, Count(*) AS Learners_In_Class FROM LearnersTb GROUP BY
RegisterTeacher ORDER BY 2 DESC ‘;
\\
Var
sTCode : string ;
Begin
sTCode := InputBox(‘’,’Enter Teacher code:’,’CV’);
sSQL:= ‘SELECT * FROM LeanersTb Where Teachers LIKE “%’ + sTCode + ‘%” ‘; ;