For more references, consider consulting the MySQL documentation
SELECT
statementThe SELECT
statement is probably the most used statement in SQL. It is used to select data from a database. The data returned from the query is stored in a result table that is referred to as the result-set
Basic syntax:
SELECT column1, column2, ...
FROM table1, table2, ...
[WHERE clause]
[OFFSET m] [LIMIT n]
-- selecting individual columns
SELECT column1, column2, ...
FROM table_name
-- wildcard (selects all columns of a table)
SELECT * FROM table_name
Some additional notes:
FROM
WHERE
clause is an optional part of the SELECT
query that can be used to specify various conditions to select byOFFSET
attribute
SELECT
query return records offset by the given value (i.e. after the given value)LIMIT
attribute
Suppose we have the following relational database:
CREATE TABLE branch(branch_name, branch_city, assets)
CREATE TABLE customer(customer_name, customer_street, customer_city)
CREATE TABLE loan(loan_number, branch_name, amount)
CREATE TABLE borrower(customer_name, loan_number)
CREATE TABLE bank_account(account_number, branch_name, balance)
CREATE TABLE depositor(customer_name, account_number)
Complete the following tasks:
In your own time, play this incredibly fun game with a friend where one person makes up a query and have the other person write relational algebra to retrieve the data
Consider the following relations:
Accounts
acctNo | type | balance |
---|---|---|
12345 | savings | 12000 |
23456 | chequing | 1000 |
34567 | savings | 0 |
Customers
firstName | lastName | idNo | account |
---|---|---|---|
Eugene | Krabs | 420-699 | 12345 |
Pearl | Krabs | 805-123 | 12345 |
Pearl | Krabs | 805-123 | 23456 |
Indicate the following:
Consider the following database consisting of the following four relations:
Product(maker, model, type)
PC(mode, speed, ram, hd, price)
Laptop(model, speed, ram, hd, screen, price)
Printer(mode, color, type, price)
Write relational algebra expressions for the following queries. Assume for convenience pruposes that the model numbers are unique across all the different manufacturers and across all product types
(i,j)
do not list the pair (j,i)
)