Write SQL Queries
Write SQL Queries
SQL Queries:
a. For all customers who have a loan from the bank, find their names and loan numbers with the
attribute loan_number replaced by loan_id.
SELECT customer_name, loan_number AS loan_id
FROM borrower;
Explanation:
This query selects customer_name and renames loan_number to loan_id from the borrower table, which
contains all customers with loans.
b. Find the names of all customers whose street address includes the substring ‘Main’.
SELECT customer_name
FROM customer
WHERE customer_street LIKE '%Main%';
Explanation:
The LIKE operator with % wildcards checks if customer_street contains the substring "Main" anywhere in the
string.
c. List loan data, ordered by decreasing amounts, then increasing loan numbers.
SELECT *
FROM loan
ORDER BY amount DESC, loan_number ASC;
Explanation:
ORDER BY amount DESC sorts loans by amount from highest to lowest.
loan_number ASC sorts ties in amount by loan_number from smallest to largest (ascending is optional
but explicit).
a. Retrieve the name and address of all employees who work for the ‘Research’ department
Assuming that the foreign key in EMPLOYEE referencing the department is named #DNO and matches
DNUMBER in DEPARTMENT, you can write the query as follows:
SELECT e.FNAME, e.MINIT, e.LNAME, e.ADDRESS
FROM EMPLOYEE e
JOIN DEPARTMENT d ON e.#DNO = d.DNUMBER
WHERE d.DNAME = 'Research';
Alternatively, using the older implicit join syntax:
SELECT e.FNAME, e.MINIT, e.LNAME, e.ADDRESS
FROM EMPLOYEE e, DEPARTMENT d
WHERE e.#DNO = d.DNUMBER
AND d.DNAME = 'Research';
b. Retrieve the birthdate and address of the employee(s) whose name is ‘Raj V Naik’
Assuming that the employee's first name is stored in FNAME, middle initial in MINIT, and last name in
LNAME, you can use:
SELECT BDATE, ADDRESS
FROM EMPLOYEE
WHERE FNAME = 'Raj'
AND MINIT = 'V'
AND LNAME = 'Naik';
Note: If the employee’s full name is stored as a single field in another design, you would adjust the WHERE
clause accordingly.
c. Retrieve all the attributes of an EMPLOYEE and the attributes of the DEPARTMENT he or she
works in for every employee of the ‘Research’ department
You can join the two tables to get all the information. Using the explicit JOIN syntax:
SELECT *
FROM EMPLOYEE e
JOIN DEPARTMENT d ON e.#DNO = d.DNUMBER
WHERE d.DNAME = 'Research';
Or, if you prefer listing out all columns (which is a good practice in production to avoid ambiguity), you might
write:
SELECT e.FNAME, e.MINIT, e.LNAME, e.SSN, e.BDATE, e.ADDRESS, e.SEX, e.SALARY,
e.#SUPERSSN, e.#DNO,
d.DNAME, d.DNUMBER, d.#MGRSSN, d.MGRSTARTDATE
FROM EMPLOYEE e
JOIN DEPARTMENT d ON e.#DNO = d.DNUMBER
WHERE d.DNAME = 'Research';
These queries assume that the relationships between the tables are as described. Adjust column names or use
proper escaping as required by your SQL environment.
Consider the following relational schema.
Suppliers(sid, sname, address)
Parts(pid, pname, address)
Catalog(sid, pid, cost)
(i) Write relational algebra query to find the names of suppliers who supply
some red part
(ii) Write relational algebra query to find the sid of suppliers who supply
some red or green parts
(i) Find the names of suppliers who supply some red part
We need to:
1. Identify parts that are red.
2. Find the catalog entries for those red parts.
3. Get the corresponding supplier information, and finally,
4. Project the supplier names.
One correct relational algebra expression is:
πsname(Suppliers ⋈Suppliers.sid=Catalog.sid(Catalog ⋈Catalog.pid=Parts.pid σpname=′red′(Parts)))\
pi_{sname}\Bigl( Suppliers \ \bowtie_{Suppliers.sid = Catalog.sid} \Bigl( Catalog \ \bowtie_{Catalog.pid =
Parts.pid} \ \sigma_{pname = 'red'}(Parts) \Bigr) \Bigr)
Explanation:
σpname=′red′(Parts)\sigma_{pname = 'red'}(Parts) selects all parts that are red.
Joining with Catalog on pidpid gives the catalog entries for red parts.
Joining with Suppliers on sidsid gives the supplier details.
Finally, πsname\pi_{sname} projects only the supplier names.
(ii) Find the sid of suppliers who supply some red or green parts
Here, we want the supplier ids (sidsid) for those who supply either a red part or a green part. We can do this in
one of two equivalent ways. One way is to select the parts that are either red or green and then join with the
Catalog. Another way is to compute the union of the two separate cases. Both are acceptable.
Method 1 (Using a single selection):
πsid(Catalog ⋈Catalog.pid=Parts.pid σpname=′red′∨pname=′green′(Parts))\pi_{sid}\Bigl( Catalog \ \
bowtie_{Catalog.pid = Parts.pid} \ \sigma_{pname = 'red' \lor pname = 'green'}(Parts) \Bigr)
Explanation:
σpname=′red′∨pname=′green′(Parts)\sigma_{pname = 'red' \lor pname = 'green'}(Parts) selects parts
that are either red or green.
The join with Catalog finds the supplier–part pairs for those parts.
πsid\pi_{sid} projects the supplier ids.
Method 2 (Using union):
πsid((σpname=′red′(Parts) ⋈Parts.pid=Catalog.pid Catalog) ∪ (σpname=′green′(Parts) ⋈Parts.pid=Catalog.pid
Catalog))\pi_{sid}\Bigl((\sigma_{pname = 'red'}(Parts) \ \bowtie_{Parts.pid = Catalog.pid} \ Catalog) \ \cup \
(\sigma_{pname = 'green'}(Parts) \ \bowtie_{Parts.pid = Catalog.pid} \ Catalog)\Bigr)
Explanation:
The left part selects catalog entries for red parts.
The right part selects catalog entries for green parts.
Their union gives all catalog entries for red or green parts.
Finally, projecting sidsid yields the desired result.
Any of the above formulations is a correct relational algebra expression for the given queries.
Consider the following employee database. Give expression in tuple rela-
tional calculus for each of the following queries.
employee(employee_name,street,city)
works(employee_name,company_name,salary)
company(company_name,city)
manages(employee_name,manager_name)
(i)Find the names of all employees who work for First Bank Corporation.
(ii)Find the names and cities of residence of all employees who work for
First Bank Corporation
(iii)Find all employees who live in the same city as that in which the com-
pany for which the work is located.
Below are one acceptable set of answers expressed in tuple relational calculus. In each case we use tuple
variables (for example, e for an employee tuple, w for a works tuple, etc.) and write the query as a set
comprehension. (You may use slightly different but equivalent notations.)
(i) Find the names of all employees who work for First Bank Corporation
We want to collect the employee names such that there is a tuple in works showing that the employee works
for “First Bank Corporation”. One correct expression is:
{ e.employee_name∣employee(e)∧∃w (
works(w)∧w.employee_name=e.employee_name∧w.company_name="First Bank Corporation") }\{\, e.\
text{employee\_name} \mid \text{employee}(e) \land \exists w\, (\, \text{works}(w) \land w.\text{employee\
_name} = e.\text{employee\_name} \land w.\text{company\_name} = \text{"First Bank Corporation"} ) \,\}
(ii) Find the names and cities of residence of all employees who work for First Bank Corporation
Now we want both the employee’s name and his/her city. We proceed similarly by joining the employee and
works relations:
{ (e.employee_name, e.city)∣employee(e)∧∃w (
works(w)∧w.employee_name=e.employee_name∧w.company_name="First Bank Corporation") }\{\, (e.\
text{employee\_name},\, e.\text{city}) \mid \text{employee}(e) \land \exists w\, (\, \text{works}(w) \land w.\
text{employee\_name} = e.\text{employee\_name} \land w.\text{company\_name} = \text{"First Bank
Corporation"} ) \,\}
(iii) Find all employees who live in the same city as that in which the company for which the work is
located
This query requires that for an employee e who works for some company c (via a tuple w in works), the
employee’s city (from employee) equals the city of the company (from company). One correct expression is:
{ e∣employee(e)∧∃w∃c (
works(w)∧company(c)∧w.employee_name=e.employee_name∧w.company_name=c.company_name∧e.city
=c.city) }\{\, e \mid \text{employee}(e) \land \exists w \exists c\, (\, \text{works}(w) \land \text{company}(c) \
land w.\text{employee\_name} = e.\text{employee\_name} \land w.\text{company\_name} = c.\text{company\
_name} \land e.\text{city} = c.\text{city} ) \,\}
Explanation
1. (i) and (ii):
o We start with a tuple from employee (variable e).
o We require the existence of a corresponding tuple w in works that relates the employee to the
company “First Bank Corporation.”
o In (i) we project just the employee name; in (ii) we project both the employee name and the city
from the employee tuple.
2. (iii):
o Again, we begin with a tuple e from employee.
o We then require that there exists a tuple w in works (linking an employee with a company) and
a tuple c in company such that:
w matches e (i.e. the employee worked for the company),
w’s company name matches c’s company name, and
the city where e lives is the same as the city where the company c is located.
Any expressions equivalent to the above answers are correct.
Consider the following schema and solve following queries using SQL.
(i) Get duty allocation details for emp_no 123461 for the month of april
1986.
(ii) Get employees whose rate of pay is more than or equal to the rate of pay
of employee ‘XYZ’.
(iii) Get the names and pay rates of employees with emp_no less than
123460 whose rate of pay is more than the rate of payoff atleast one em-
ployee with emp_no greater than or equal to 123460.
Below are one acceptable set of SQL queries for the given problems. In the examples below, it is assumed that
the attribute day in the duty_allocation table is of a date type. (Also, note that table names containing hyphens
—such as duty-allocation—are typically not allowed in SQL unless quoted; here we assume the table is
named duty_allocation.)
(i) Get duty allocation details for emp_no 123461 for the month of April 1986
Assuming that day is of date type, one way is to use the BETWEEN operator:
SELECT *
FROM duty_allocation
WHERE emp_no = 123461
AND day BETWEEN '1986-04-01' AND '1986-04-30';
Alternatively, if your SQL dialect supports date functions, you could write:
SELECT *
FROM duty_allocation
WHERE emp_no = 123461
AND EXTRACT(YEAR FROM day) = 1986
AND EXTRACT(MONTH FROM day) = 4;
(ii) Get employees whose rate of pay is more than or equal to the rate of pay of employee ‘XYZ’
This query uses a subquery to retrieve the pay rate for employee 'XYZ':
SELECT *
FROM employee
WHERE pay_rate >= (
SELECT pay_rate
FROM employee
WHERE name = 'XYZ'
);
Note: This assumes that the name 'XYZ' is unique in the employee table. If not, you may need to further
qualify the employee (for example, by emp_no) or handle multiple rows appropriately.
(iii) Get the names and pay rates of employees with emp_no less than 123460 whose rate of pay is more
than the rate of pay of at least one employee with emp_no greater than or equal to 123460
This query uses an EXISTS subquery to ensure that for each employee ee with emp_no < 123460, there exists
at least one other employee e2e2 (with emp_no ≥ 123460) such that ee’s pay rate is greater than e2e2’s pay
rate:
SELECT e.name, e.pay_rate
FROM employee e
WHERE e.emp_no < 123460
AND EXISTS (
SELECT 1
FROM employee e2
WHERE e2.emp_no >= 123460
AND e.pay_rate > e2.pay_rate
);
Any of the above SQL queries correctly addresses the corresponding parts of the problem.
Below are the SQL queries for the given problems based on the schema:
Weather(city, temperature, humidity, condition)
Location(city, country)
(i) Find all the tuples having temperature greater than that of Paris
Assuming that the temperature for Paris is stored in the Weather table, you can use a subquery to compare
each tuple's temperature with that of Paris:
SELECT *
FROM Weather
WHERE temperature > (
SELECT temperature
FROM Weather
WHERE city = 'Paris'
);
(ii) Find the names of those cities with temperature and condition whose condition is neither Sunny nor
Cloudy but temperature must be greater than 70
Here, we need to select the city, temperature, and condition from Weather where the condition is not 'Sunny'
or 'Cloudy' and the temperature exceeds 70:
SELECT city, temperature, condition
FROM Weather
WHERE condition NOT IN ('Sunny', 'Cloudy')
AND temperature > 70;
(iii) Find all the cities with temperature, condition and humidity, whose humidity is in the range of 63 to
79
This query selects the desired columns from Weather and uses the BETWEEN operator to restrict humidity to
the range 63 to 79 (inclusive):
SELECT city, temperature, condition, humidity
FROM Weather
WHERE humidity BETWEEN 63 AND 79;
(3) Delete the record for the student whose name is 'Smith' and whose student number is 17:
DELETE FROM Student
WHERE name = 'Smith' AND s_no = 17;
These SQL statements correctly perform the insert, update, and delete operations as required.
Date:01-01-24
Here are the SQL queries for each of the given tasks:
(b) Find the names of suppliers who supply some red part
SELECT DISTINCT S.sname
FROM Suppliers S
JOIN Catalog C ON S.sid = C.sid
JOIN Parts P ON C.pid = P.pid
WHERE P.color = 'red';
(c) Find the sids of suppliers who supply some red or green parts
SELECT DISTINCT C.sid
FROM Catalog C
JOIN Parts P ON C.pid = P.pid
WHERE P.color IN ('red', 'green');
(d) Find the sids of suppliers who supply some red part OR are located at "221 Packer Ave"
SELECT DISTINCT S.sid
FROM Suppliers S
LEFT JOIN Catalog C ON S.sid = C.sid
LEFT JOIN Parts P ON C.pid = P.pid
WHERE P.color = 'red' OR S.address = '221 Packer Ave';
(e) Find the sids of suppliers who supply some red part AND some green part
SELECT DISTINCT C1.sid
FROM Catalog C1
JOIN Parts P1 ON C1.pid = P1.pid
WHERE P1.color = 'red'
AND EXISTS (
SELECT 1 FROM Catalog C2
JOIN Parts P2 ON C2.pid = P2.pid
WHERE C1.sid = C2.sid AND P2.color = 'green'
);
These SQL queries should correctly perform the required database operations. 🚀 Let me know if you
need any modifications!
Here are the Relational Algebra Queries for the given tasks based on the Account table schema:
Schema:
Account (Acno, AcName, Br_name, Amount)
(c) To display the tuples for the account where Amount > 25000 and Br_name = "Dharashiv"
📌 Relational Algebra Expression:
σAmount>25000∧Br_name=′Dharashiv′(Account)\sigma_{Amount > 25000 \land Br\_name =
'Dharashiv'} (Account)
(Selection is used to filter based on the given conditions.)
(d) To display Acno, AcName, Amount from Account where Amount > 50000
📌 Relational Algebra Expression:
πAcno,AcName,Amount(σAmount>50000(Account))\pi_{Acno, AcName, Amount} (\sigma_{Amount
> 50000} (Account))
(First, selection filters records with Amount > 50000, then projection retrieves only the required
attributes.)
These relational algebra expressions correctly retrieve the required information from the Account table.
🚀 Let me know if you need further clarifications!