Querying Relational Databases with SQL Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Add a tuple in the Student table/ insert rows

A

INSERT
INTO Student (mn, name, email, age)
VALUES (‘s1253477’, ‘Jenny’, ‘jenny@sms.ed.ac.uk’, 23)
• The above statement adds a tuple in the Student table.
• We could omit the list of column names and simply list
the values in the appropriate order, but it is good
practice to include column names.

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

Deleting rows

A

DELETE
FROM Student
WHERE name = ‘Alan’

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

Updating rows

A
We can update the column values in an
existing row using the UPDATE command
UPDATE Student
SET name = ‘Alan’
WHERE mn = ‘s1428571’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What questions can we ask to the database?

A

Which students are older than 19?
 What are the names of all students taking the
Medical Informatics course?
 What is the average age of all students born in
Europe who are taking the Medical Informatics
course but not the Advanced Databases course?

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

How to query returns all students older than

19.

A

• The following query returns all students older than
19.
SELECT *
FROM Student
WHERE age > 19
• The * means that the table returned has the same
schema as Students.

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

What is SQL query syntax?

A

SELECT [DISTINCT] field-list
FROM table-list
[ WHERE qualification ]
• Anything in [square brackets] is optional.
• SELECT: the columns to be retained in the result
• FROM: the tables from which to take the data
• WHERE: conditions that should hold for the records
to be picked out

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

What can we use instead of *

A

Instead of using *, we can explicitly specify the
list of fields to be returned. These could be in
a different order than in the original table
SELECT mn, name, email, age
FROM Student
WHERE age > 19

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

How can we specify which tables the fields are from

A
We can specify which tables the fields are
from.
• This is particularly useful when the FROM clause includes several tables. 
SELECT Student.mn, Student.name,
Student.email, Student.age
FROM Student
WHERE Student.age > 19
Locally abbreviating names= Student S
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to select only a subset of the

fields of each selected tuple

A
SELECT S.name
FROM Student S
WHERE S.age > 19
• In this case, the table returned has a different
schema to that in Student
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How not to specify a condition through

the WHERE-part of the query

A

SELECT age

FROM Student

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

How to remove any duplicates from

the returned records

A

By using DISTINCT, we remove any duplicates from
the returned records.
SELECT DISTINCT age
FROM Student

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

How to query returns the email
addresses of all students taking Medical
Informatics

A
SELECT S.email
FROM Student S, Takes T, Course C
WHERE S.mn = T.mn
AND T.cid = C.cid
AND C.title = ‘Medical Informatics’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to query evaluation

A
  1. Take all rows from the tables.
  2. Keep only the row combinations that satisfy the
    qualification conditions.
  3. Return the specified columns.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The three set-operation constructs

that extend the basic form of a query:

A

UNION: A or B
 INTERSECT: A and B
 EXCEPT: A but not B

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

Example of union

A

Find the email addresses of all students taking

Medical Informatics or Advanced Databases

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

Example of intersect

A

Find the email addresses of all students taking

Medical Informatics and Advanced Databases.

17
Q

Example of except

A

Find the email addresses of all students taking

Medical Informatics but not Advanced Databases

18
Q

What are nested queries?

A

• Queries that have other queries embedded within
them.
• The idea is to use the result of one query to build
another one.
• The following query returns the names of all
students that have a mark higher than 70 in any
course.

19
Q

Example of nested queries

A
The following query returns the names of all
students that have a mark higher than 70 in any
course.
SELECT DISTINCT S.name
FROM Student S
WHERE S.mn IN ( SELECT T.mn
FROM Takes T
WHERE T.mark > 70 )
20
Q

What are aggregate operators in SQL?

A

SQL also allows us to compute aggregate values

rather than simply retrieve data.

21
Q

What are the 5 aggregate operations available?

A

COUNT([DISTINCT] field-name): The number of (unique)
values in a particular field
 SUM([DISTINCT] field-name): The total of all (unique)
values in a particular field
 AVG([DISTINCT] field-name): The mean of all (unique)
values in a particular field
 MAX(field-name): The maximum value in a particular field
 MIN(field-name): The minimum value in a particular field