0% found this document useful (0 votes)
4 views3 pages

SQL notes

The document provides an overview of SQL, focusing on the SELECT statement and its components such as FROM, WHERE, and LIKE clauses. It includes examples of queries for filtering data from a CD database and tips for using SQL effectively, including handling multiple tables and aggregate functions. Additionally, it highlights the use of wildcards in LIKE statements and offers guidance on variable usage in SQL queries.

Uploaded by

kiara.amy2403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

SQL notes

The document provides an overview of SQL, focusing on the SELECT statement and its components such as FROM, WHERE, and LIKE clauses. It includes examples of queries for filtering data from a CD database and tips for using SQL effectively, including handling multiple tables and aggregate functions. Additionally, it highlights the use of wildcards in LIKE statements and offers guidance on variable usage in SQL queries.

Uploaded by

kiara.amy2403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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 + ‘%” ‘; ;

You might also like