|
Persons |
|||||
|
id |
name |
address |
age |
eyeColor |
gender |
|---|---|---|---|---|---|
|
BankAccounts |
|
|
id |
balance |
|---|---|
|
AccountOf |
||
|
id |
person_id → Persons |
account_id → BankAccounts |
|---|---|---|
Write a query that returns the names of all people that have less than half of the wealth of the richest person. We define the wealth of a person as the total money on all of his/her accounts.
SELECT p.name FROM BankAccount b INNER JOIN AccountOf a ON b.id
= a.account_id INNER JOIN Persons p ON p.id = a.person_id
GROUP BY a.person_id
HAVING SUM(b.balance) < (SELECT SUM(b.balance) FROM BankAccounts
b INNER JOIN AccountOf a ON b.id = a.account_id GROUP BY
a.person_id ORDER BY SUM(b.balance))/2;
Explanation
The query above will fetch the names of all the persons having the total balance in all the accounts less than half the total balance of the petson having highest balance.
We have used the concept to subquery for the query above. A subquery is actually a query inside another parent query.
The subquery used here will give us the highest total wealth of a person.
We have now used this value to compare with the total wealths of the other persons.
Inner join is used to join the three tables.
Group by is used to group all the accounts of a person
Sum aggregate function is used to find the sum of balance of all accounts for a user.
Having is used to compare total wealth of each user with the half of highest wealth.
Persons id name address age eyeColor gender BankAccounts id balance AccountOf id person_id → Persons account_id...
1. Answer the questions with respect to the table below. users gender id name age occ_id city_id John 25 M 3 1 2. Sara 20 F 3 4 3 Victor 31 M 2 LO 4 Jane 27 F 1 3 occupation id name 1 2 Software Engineer Accountant Pharmacist Library Assistant 3 City id name 1 2 Halifax Calgary Boston 3 4 New York 5 Toronto (b) [2.5+2.5+5) Write query to make a copy of 'users' table known as 'users_new'....
Suppose that there are three relationships in a database: Student relationship: S(SNO, SNAME, AGE, GENDER, DEPT) Enroll relationship: SC(SNO, CNO, GRADE) Course relationship: C(CNO, CNAME) Here, the abbreviation and their full name correspond as: SNO Student Id number DEPT Student department SNAME Student name CNO Course number AGE Student age GRADE Course grade for certain student GENDER Student gender CNAME Course name Query the following in T-SQL format: Query the id number of the students who at least enrolled C2...
Consider a database with the following schema: Person ( name, age, gender ) name is a key Frequents ( name, pizzeria ) (name, pizzeria) is a key Eats ( name, pizza ) (name, pizza) is a key Serves ( pizzeria, pizza, price ) (pizzeria, pizza) is a key Write SQL clauses for the queries given: e. Find all pizzerias that are frequented by only females or only males. f. For each person, find all pizzas the person eats that are...
2. Write a program to prepare email address with name of person, email ID. This program should collect number of persons with their details menstion in the above. Finally, print all the registerd preson with their details and also print selected particular person's emails. 1. Write a program to prepare departmental store records with item name, number of items. This program should collect number of items with its details menstion in the above. Finally, print all the registerd items with...
PYTHON*************** Create a Person Class that: Accepts First Name, Last Name, Age, and Gender Has a method that adds all persons Validate all input, not left empty for First Name, Last Name, Gender and numeric for age and throw an exception if it does not validate within the class. Create a Student Class that: Inherits the Person Class Accepts GPA Validate GPA (>= 0 and <= 4.0) and throw exception if it does not validate within the class. Has methods...
For the relation schema Player (ID, Name, Birthday, Address, Email, PhoneNumber, PlayPos) assume that ID is the primary key and no Player’s names are the same (i.e., a Players’s name can uniquely identify a Player). 1. Please write down all the non-trivial functional dependencies that hold in the schema Player. 2. Please state all the candidate keys of the schema Player.
For all problems, use the following schema: Musician(id, first_name, last_name, instrument, band_id) Band(id, name, years_together) Show(id, venue_id, date) Played_in(band_id, show_id) Venue(id, name, address) Album(id, name, year, band_id, genre_id) Genre(id, name, description) Song(id, name, album_id) Primary keys are in bold, foreign keys are in italics. For each problem, write a query once using relational algebra, and again using SQL. 12. Find the names of all bands that have a member who plays "Guitar", OR have a member that plays "Keyboard". 13....
14. Write a query that lists the trip id, trip name and state
for all trips in New Hampshire. Run "Explain" against this query.
Now create an index on the state. Re-run your query and the
"Explain" Are there differences in the Explain results? Why or why
not?
Customer Customer Num LastName FirstName Address City State PostalCode Phone Reservation ReservationID TripID Trip Date Num Persons Trip Price Other Fees Customer Num Trip Guide TripID Guide Num LastName Trip Name Start...
31. (15 pts) Consider the following relations and relationship: Student (studentID, name, address, gender, major) Course (courselD, title, hour, department) Enrollment (StudentID, courselD, date Write the SQL statements to perf orm the following operations: List all the students' information for those majoring in "Computer Science". (2 pts) List the majors, and the numbers of the students in every major. (2 pts) List the departments, and the total number of hours of the courses offered by every department. (2 pts) List...
C++ There is a class called Person which has name and age as private variables. Another class called Student, which is derived from person with gpa and id as variables. name is a string, age and id are integers and gpa is a double... All of them are private variables. age, gpa and id should be generated randomly when the object is created with the following ranges: age 20 to 32 gpa 0.0 to 4.0 // this one is tricky...