Question

code the following SQL questions Find the trips where the distance is greater than the average...

code the following SQL questions

  1. Find the trips where the distance is greater than the average distance for all trips.
  2. List the name of each guide who is assigned to a trip.

     

Homework Answers

Answer #1

Solution:

Creating tables:

create table trip (tripid int primary key,distance int,guideid int);

create table guide(guideid int primary key,guidename char(20));

alter table trip add foreign key(guideid) references guide(guideid);

Inserting rows into table:

insert into guide values (1,"Adam"),(2,"Charlie"),(3,"Roman"),(4,"Lynn"),(5,"Rajat");

insert into trip values (100,400,1),(101,300,3),(102,150,2),(103,500,5),(104,273,4),(105,146,3);

Displaying rows of the tables:

select * from trip;

select * from guide;

Query 1:

select tripid,distance from trip where distance > (select avg(distance) from trip);

Explanation:

In above query first we are retrieving average distance from trip table and checking each trip distance is greater than average or not if yes add that record to result else continue.

Query 2:

select g.guideid,tripid,guidename from guide g join trip t on g.guideid=t.guideid;

Explanation:

In above query we are joining both guide and trip tables and retrieving the guides who assigns to a trip.

Code and Output Screenshots:

Note: if you have any queries please post a comment thanks a lot..always available to help you..

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please provide SQL code for following question, and please make sure your answer is LEGIBLE. Thank...
Please provide SQL code for following question, and please make sure your answer is LEGIBLE. Thank you. Retrieve a list of employees and the projects they are working on, ordered by department, and within each department, ordered alphabetically by last name, first name
Give the relational algebra, tuple calculus, and SQL expressions for each using the below schema: SPERSON(EmpID,...
Give the relational algebra, tuple calculus, and SQL expressions for each using the below schema: SPERSON(EmpID, Name, Dept) TRIP(TripID, EmpID, ToCity, StartDate, EndDate) EXPENSE(ExpID, TripID, AccountID, Amount) a) List the names and employee ID of a salesperson. b) List employee ID and employee names that took a trip(s) to Charleston city. c) List the names of all employees who have spent more than $500 on their trip. d) List employees' names and their IDs that have never taken a trip....
Write the following questions as queries in SQL. Use only the operators discussed in class (in...
Write the following questions as queries in SQL. Use only the operators discussed in class (in particular, no outer joins or windows). Type your answers. Before starting, make sure you understand the schema of the database. If you are in doubt about it, please ask the instructor. Assume a database with schema ACTOR(name,age,address,nationality) MOVIE(title,year,genre,budget,director-name,studio) APPEARS(name,title,salary) 1. Find the title, director and studio of the most expensive movie of 2010 (note: there can be ties!). 2. Find the title, director and...
Using the Company database in Oracle, construct SQL queries for the following (note: I will make...
Using the Company database in Oracle, construct SQL queries for the following (note: I will make the files to create and populate the Company database available on Isadore shortly): List the last name and address of managers who have a dependent with the same first name as themselves. Retrieve the names of all employees who are directly supervised by ‘Franklin Wong’. Retrieve the names of employees in the Research department who work more than 20 hours per week on the...
Given the following two related tables, write the SQL SELECT statement to solve the following queries...
Given the following two related tables, write the SQL SELECT statement to solve the following queries about developers (Dev) and programs (Prog). The id column in Dev and Prog is the relation between the two tables. Dev(id, name, dept, salary) Prog(id, title, language, lines, size); 1. List name of Devs who have written more than 1000 line programs 2. List title of all programs written in C++ language 3. List name of all developers who have written C++ programs
Specify the following queries in SQL on the COMPANY database. Show the result of each query....
Specify the following queries in SQL on the COMPANY database. Show the result of each query. Retrieve the names of all employees in department 5 who work more than 10 hours per week on the ‘ProductX’ project. List the names of all employees who have a dependent with the same first name as themselves. Find the names of all employees who are directly supervised by ‘Franklin Wong’. Specify the following updates using the SQL update commands. Show the state of...
The following code should print X is greater than 0. However, the code has errors. Fix...
The following code should print X is greater than 0. However, the code has errors. Fix the code so that it compiles and runs correctly. (Rewrite the program with all the fixes) public class Test1 { public static void main(String[] args) { int x = 3; if (x > 0 System.out.println("x is greater than 0") else System.out.println(x is less than or equal 0"); } } Must be in java and easy to understand grade 11 course
Run the following queries from an SQL database containing the following normalized entities: STORE(SID(pk), SName, SCity,...
Run the following queries from an SQL database containing the following normalized entities: STORE(SID(pk), SName, SCity, SState, SSqFeet) ITEM(ISerialNum(pk), IName, IType, IPrice) SELLS(SID, ISerialNum) (concatenated pk and both fk) 1. Show all the items with name, type, and price and the store that sold them, order by most expensive item first. 2. Show all stores that sold a specific type of an item. For example: show all stores that sold cooking items. 3. Show all stores that have a square...
Consider the following relational schema (the primary keys are underlined and foreign keys are italic) ITEM(ItemName,...
Consider the following relational schema (the primary keys are underlined and foreign keys are italic) ITEM(ItemName, ItemType, ItemColour) DEPARTMENT(Deptname, DeptFloor, DeptPhone, Manager) EMPLOYEE(EmpNo, EmpFname, EmpSalary, DeptName, SupervisedBy) SUPPLIER(SupNo, SupName) SALE(SaleNo, SaleQty, ItemName, DeptName) DELIVERY(DeliNo, DeliQty, ItemName, DeptName, SupNo) Write the SQL statements for the following queries: C1. Find the names of items sold on first and second floors. [1 mark] C2. For each department, list the department name and average salary of the employees where the average salary of the...
Question 1: Group by and Aggregates: Write SQL statements to answer the following questions using Assignment...
Question 1: Group by and Aggregates: Write SQL statements to answer the following questions using Assignment 4’s schema (Customer-Invoice-Line-Product-Vendor). Make sure that your SQL script runs without any errors. Submit your answers in a .SQL file. 1 (2 Points) - Find the count of distinctvendors thatsupplied products that are priced lowerthan 185? 2 (2 Points) - For each vendor, find their product that has the lowest product quantity. Your output should include vendor code, vendor name, product description and product...