L09-SQL Summary
L09-SQL Summary
Dr Renata Borovica-Gajic
Lecture 9
SQL Summary
• Comparison:
Operator Description
= Equal to
• Logic:
• AND, NOT, OR
• Example: SELECT * FROM Furniture WHERE ((Type=“Chair” AND
Colour = “Black”) OR (Type = “Lamp” AND Colour = “Black”))
• UNION
– Shows all rows returned from the queries (or tables)
• INTERSECT
– Shows only rows that are common in the queries (or the tables)
• [UNION/INTERSECT] ALL
– If you want duplicate rows shown in the results you need to use the
ALL keyword.. UNION ALL etc.
• IN / NOT IN
– Used to test whether the attribute is IN/NOT IN the subquery list
• ANY
– True if any value returned meets the condition
• ALL
– True if all values returned meet the condition
• EXISTS
– True if the subquery returns one or more records
SellerID INT
BuyerID INT
ArtefactID INT
Seller
Buyer
Offer
List the BuyerID, Name and Phone number for all bidders on artefact 1
Offer Buyer
Result
Offer Artefact
Result
• List the BuyerID, Name and Phone number for all bidders on
artefact 1
Equals to
Result
• Any: must satisfy at least one of the inner conditions (any of)
SELECT empno, sal Equiv. SELECT empno, sal
FROM emp FROM emp
WHERE sal > ANY (200, 300, 400); WHERE sal > 200 OR sal > 300 OR sal> 400;
• Example: Increase all salaries greater than $100000 by 10% and all other
salaries by 5%
• REPLACE
– REPLACE works identically as INSERT
• Except if an old row in a table has a key value the same as the new row
then it is overwritten…
• DELETE
– The DANGEROUS command – deletes ALL records
• Any relation that is not in the physical models, but is made available to the
“user” as a virtual relation is called a view.
• Views are good because:
– They help hide the query complexity from users
– They help hide data from users
• Different users use different views
– Prevents someone from accessing the employee tables to see salaries for
instance
• One way of improving database security
• Create view statement:
CREATE VIEW nameofview AS validsqlstatement
• TRUNCATE
– Same as DELETE * FROM table;
– Faster but cannot ROLL BACK a TRUNCATE command
• Have to get data back from backup…
• DROP
– Potentially DANGEROUS
• Kills a relation – removes the data, removes the relation
– There is NO UNDO COMMAND! (have to restore from backup)
• DROP TABLE TableName
• DCL
– Users and permissions
• CREATE USER, DROP USER
• GRANT, REVOKE
• SET PASSWORD
• Other Commands
– Database administration
• BACKUP TABLE, RESTORE TABLE
• ANALYZE TABLE
– Miscellaneous
• DESCRIBE tablename
• USE db_name
• They are typically called ‘Database Administration Statements’
• It’s going to be critical for you to think like SQL to handle the
queries you will need to write…
What is the phone number of the instructor who teaches a course that costs
over 10000$ attended by studentID 202.
https://www.youtube.com/watch?v=uRdIdd-
UkTc&index=7&list=PLdQddgMBv5zHcEN9RrhADq3CBCol
hY2hl