Lecture 7: SQL + RA Flashcards

1
Q

Explain the use of the select command

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write / recite the select command general syntax

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the purpose of Distinct?

A

Use distinct to eliminate duplicates.

e.g select propertyNo FROM viewing. —>

SELECT distinct propertyNo from viewing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to use “AS”

A

creating a new column

SELECT staffNo, fName, lName, salary/12 AS monthlySalary FROM staff

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Name and give an example of a comparison search condition

A

List all staff with a salary greater than 10,000. Using WHERE condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you write “List al staff with a salary between 20,000 and 30,000

A

select staffNo, fName, lName, position, salary
from staff
where salary BETWEEN 2000 AND 30000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are some other range search conditions

A
  • NOT BETWEEN
    and
  • logical operators (< , >, =>, =
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to use function “IN”

A

select staffNo, fName
FROM staff
WHERE position IN (‘Manager’,’position’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you use the negated version NOT IN?

A

select staffNO, fName, lName, position
FROM staff
WHERE position NOT IN (‘Manager’,’Supervisor’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Whats another option for writing “AND” ?

A

also use OR logical operator

SELECT staffNo, fName, position
FROM staff
WHERE position = ‘Manager’ OR
position = ‘Supervisor’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Pattern matching operator is?

A

LIKE operatore is unique because it allows us to use wildcard characters search for patterns in strings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are SQL’s two special matching symbols?

A

% sequence of zero or more characters

_ (underscore) are single character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Use % in a SQL statement

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

how to use the underscore matching pattern

A

select fName, lName, branchNo
FROM staff WHERE
lName LIKE ‘_o%’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how to search a NULL condition

A

select * from viewing WHERE comment LIKE ‘’;

or
WHERE comment is NULL;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to single columns ordering?

A

USE DESC or ASC;

e.g. order by salary DESC;

17
Q

Provide example of multiple columns ordering

A

ORDER BY type

18
Q

How to arrange in order of rent, specify minor order?

A

select propertyNo, type, room, rent
FROM propertyForRent
ORDER BY type, rent DESC

19
Q

Why theory (mathematical grounding/RA0

A
  • it provides a way of thinking about operations & results
  • it is general
  • used as a basis to implement and optimise queries in the query processing and optimisation modules
20
Q

Describe Relational Algebra (RA)

A
  • DML can be expressed in a formal way using RA
  • RA is a procedural language that defines database operations in terms of algebraic expressions
  • RA operations work on one or more relations and produce a new relation f(R) = S
  • the result of operation can be an input of another operation, therefore effectively nesting/combining operations
21
Q

Why is RA important?

A
  1. it provides a formal foundation for relational model operations
  2. it is used as a basis for implementing and optimising queries in a RDBMS
  3. some of its concepts are incorporated into SQL
22
Q

Only List the set operations

A
selection  (where)
projection (from)
cartesian product
union
set different
23
Q

List operations specific to RDBMS

A

join
intersection
division

24
Q

what are the logical operator symbols for:
AND
OR
NOT

A

AND ^
OR ^(OPP)
NOT ~