Lecture 7: SQL + RA Flashcards
Explain the use of the select command
- Use to retrieve and display data from the DB
- extremely powerful and has many forms
- most frequently used SQL command
- easy to learn but requires some practice to master it
Write / recite the select command general syntax
select (distinct | ALL) or (column AS newName)..
FROM tableName
WHERE condition
GROUP BY columnList HAVING condition
ORDER BY columnList
Order cannot be changed
Only select and from are mandatory
What is the purpose of Distinct?
Use distinct to eliminate duplicates.
e.g select propertyNo FROM viewing. —>
SELECT distinct propertyNo from viewing
How to use “AS”
creating a new column
SELECT staffNo, fName, lName, salary/12 AS monthlySalary FROM staff
Name and give an example of a comparison search condition
List all staff with a salary greater than 10,000. Using WHERE condition
How do you write “List al staff with a salary between 20,000 and 30,000
select staffNo, fName, lName, position, salary
from staff
where salary BETWEEN 2000 AND 30000
What are some other range search conditions
- NOT BETWEEN
and - logical operators (< , >, =>, =
How to use function “IN”
select staffNo, fName
FROM staff
WHERE position IN (‘Manager’,’position’)
How can you use the negated version NOT IN?
select staffNO, fName, lName, position
FROM staff
WHERE position NOT IN (‘Manager’,’Supervisor’)
Whats another option for writing “AND” ?
also use OR logical operator
SELECT staffNo, fName, position
FROM staff
WHERE position = ‘Manager’ OR
position = ‘Supervisor’
Pattern matching operator is?
LIKE operatore is unique because it allows us to use wildcard characters search for patterns in strings
What are SQL’s two special matching symbols?
% sequence of zero or more characters
_ (underscore) are single character
Use % in a SQL statement
select ownerNo, address, fName
FROM privateOwner
Where address LIKE ‘%Glasgow%’
returns characters of any length containing glasgow
OR
select fName, lName
WHERE fname LIKE ‘J%’
how to use the underscore matching pattern
select fName, lName, branchNo
FROM staff WHERE
lName LIKE ‘_o%’
how to search a NULL condition
select * from viewing WHERE comment LIKE ‘’;
or
WHERE comment is NULL;