A couple quick SQL questions
PostPosted: Sun Nov 02, 2008 7:10 pm
Hiya, folks. I'm taking an intro-to-databases course this semester, and I'm having a little trouble with a couple of questions on my homework. I'm hoping that there's someone in here who has a little more experience with this than I do...
Both of these questions have to be programmed using nothing but SQL queries; while I will be using JDBC/ODBC to implement them, my instructor insists that we come up with the answers using just raw SQL and the SQL aggregate functions (MAX, MIN, AVG, etc...).
So, let me set the two problems up. They both refer to the same database tables...
If this isn't clear in English, here's the exact SQL queries I executed to create the tables:
So that's the setup I'm running with on these questions. And so... here are the questions.
"Find the average number of cars that each sales_rep sells."
And...
"For each car, find the customer who has driven that car the maximum number of times."
For the first one, I think that I'd like to count the number of cars being sold and divide that by the number of sales_reps... but I'm not sure how to do that in SQL. (obviously, this would be trivial if I did it in the Java program, but I'm not supposed to do it that way.)
The second one, I'm really not sure how to go about it. I know that I should probably use a GROUP-BY-clause to get all the rows grouped based on who test-drove the cars, but how to figure out which person's license appears most frequently in the table is beyond me.
Any tips on this would be greatly appreciated!
Both of these questions have to be programmed using nothing but SQL queries; while I will be using JDBC/ODBC to implement them, my instructor insists that we come up with the answers using just raw SQL and the SQL aggregate functions (MAX, MIN, AVG, etc...).
So, let me set the two problems up. They both refer to the same database tables...
Consider the following schema:
Sales_Rep (name, dept, phone)
Automobile (vin, make, model, color, seller, purchased_by)
FK ["Foreign Key"] seller references Sales_Rep
FK purchased_by references Customer
Customer (license, cname, address, phone)
Test_Drives (vin, license, date)
FK vin references Automobile
FK license references Customer
If this isn't clear in English, here's the exact SQL queries I executed to create the tables:
- Code: Select all
create table Sales_Rep (
name varchar(10),
dept varchar(10),
phone varchar(10),
Primary Key(name)
);
create table Customer (
license varchar(10),
cname varchar(10),
address varchar(10),
phone varchar(10),
Primary Key(license)
);
create table Automobile (
vin varchar(10),
make varchar(10),
model varchar(10),
color varchar(10),
seller varchar(10),
purchased_by varchar(10),
Primary Key(vin),
Foreign Key(seller) references Sales_Rep(name),
Foreign Key(purchased_by) references Customer(license)
);
create table Test_Drives (
vin varchar(10),
license varchar(10),
date date,
Primary Key(vin, license),
Foreign Key(vin) references Automobile(vin),
Foreign Key(license) references Customer(license)
);
So that's the setup I'm running with on these questions. And so... here are the questions.
"Find the average number of cars that each sales_rep sells."
And...
"For each car, find the customer who has driven that car the maximum number of times."
For the first one, I think that I'd like to count the number of cars being sold and divide that by the number of sales_reps... but I'm not sure how to do that in SQL. (obviously, this would be trivial if I did it in the Java program, but I'm not supposed to do it that way.)
The second one, I'm really not sure how to go about it. I know that I should probably use a GROUP-BY-clause to get all the rows grouped based on who test-drove the cars, but how to figure out which person's license appears most frequently in the table is beyond me.
Any tips on this would be greatly appreciated!