Hello, I am a total noobie at SQL and I still don't understand all the concepts. I want to see how to solve this first query and hopefully I'll be able to go from there.
I have this database:
Customer (CustNo, CustFirstName, CustLastName, CustStreet, CustCity, CustState, CustZip, CustBal)
Employee (EmpNo, EmpFirstName, EmpLastName, EmpPhone, EmpEmail, SupEmpNo, EmpCommRate)
Product (ProdNo, ProdName, ProdMfg, ProdQOH, ProdPrice, ProdNextShipDate)
OrderTbl (OrdNo, OrdDate, CustNo, EmpNo, ShpName, ShpStreet, ShpCity, ShpState, ShpZip)
OrdProd (OrdNo, ProdNo, Qty)
2 answers: first one using 3 simple subqueries nested, second one with JOIN (using no subqueries).
List the product number, name, and price of those products with a price greater than $150 and were ordered in Jan 2008 by customers with balances greater than $400.
Thank you in advance.
Need help starting an SQL query
Started by Lokantis, Oct 07 2008 12:38 PM
6 replies to this topic
#1
Posted 07 October 2008 - 12:38 PM
|
|
|
#2
Posted 08 October 2008 - 09:21 AM
You need to tell us what the structure of the db is (relationships between tables; im assuming that what you have listed is the table structure with the columns you would like returned.) If the relationships aren't set up as expected (every programmer sets them up a bit differently) than as soon as I used a join I would end up with erroneous results. If these are indeed your tables all you need to list is the primary and other keys. Unfortunately I have a heavy work load and can't guarantee that I will be able to stop back by and answer the rest of this but if I find some time I will try:) Hopefully someone else will help you out with this if you post your keys (also maybe label your tables, db's, columns, and rows as such so that we know what it is you are dealing with. Im also assuming that what you want is a simple SELECT query but if you need more than that you should specify.
#3
Posted 08 October 2008 - 10:01 AM
Lokantis said:
Customer (CustNo, CustFirstName, CustLastName, CustStreet, CustCity, CustState, CustZip, CustBal)
Employee (EmpNo, EmpFirstName, EmpLastName, EmpPhone, EmpEmail, SupEmpNo, EmpCommRate)
Product (ProdNo, ProdName, ProdMfg, ProdQOH, ProdPrice, ProdNextShipDate)
OrderTbl (OrdNo, OrdDate, CustNo, EmpNo, ShpName, ShpStreet, ShpCity, ShpState, ShpZip)
OrdProd (OrdNo, ProdNo, Qty)
2 answers: first one using 3 simple subqueries nested, second one with JOIN (using no subqueries).
List the product number, name, and price of those products with a price greater than $150 and were ordered in Jan 2008 by customers with balances greater than $400.
Thank you in advance.
Employee (EmpNo, EmpFirstName, EmpLastName, EmpPhone, EmpEmail, SupEmpNo, EmpCommRate)
Product (ProdNo, ProdName, ProdMfg, ProdQOH, ProdPrice, ProdNextShipDate)
OrderTbl (OrdNo, OrdDate, CustNo, EmpNo, ShpName, ShpStreet, ShpCity, ShpState, ShpZip)
OrdProd (OrdNo, ProdNo, Qty)
2 answers: first one using 3 simple subqueries nested, second one with JOIN (using no subqueries).
List the product number, name, and price of those products with a price greater than $150 and were ordered in Jan 2008 by customers with balances greater than $400.
Thank you in advance.
Do you understand how to do a subquery on a select? Do you understand how to do a join?
#4
Posted 08 October 2008 - 10:41 AM
I kind of understand how to do joins but I don't understand why or how it works exactly. I got this so far
SELECT DISTINCT ProdNo, ProdName, ProdPrice
FROM Product
JOIN Product ON OrdProd.ProdNo = Product.ProdNo
JOIN OrdProd ON OrderTbl.OrdNo = OrdProd.OrdNo
WHERE ProdPrice > '150' AND OrdDate = 'Jan 2008';
I know the syntax is probably wrong.
Also I forgot to specify the keys, they are the bolded words:
Customer (CustNo, CustFirstName, CustLastName, CustStreet, CustCity, CustState, CustZip, CustBal)
Employee (EmpNo, EmpFirstName, EmpLastName, EmpPhone, EmpEmail, SupEmpNo, EmpCommRate)
Product (ProdNo, ProdName, ProdMfg, ProdQOH, ProdPrice, ProdNextShipDate)
OrderTbl (OrdNo, OrdDate, CustNo, EmpNo, ShpName, ShpStreet, ShpCity, ShpState, ShpZip)
OrdProd (OrdNo, ProdNo, Qty)
SELECT DISTINCT ProdNo, ProdName, ProdPrice
FROM Product
JOIN Product ON OrdProd.ProdNo = Product.ProdNo
JOIN OrdProd ON OrderTbl.OrdNo = OrdProd.OrdNo
WHERE ProdPrice > '150' AND OrdDate = 'Jan 2008';
I know the syntax is probably wrong.
Also I forgot to specify the keys, they are the bolded words:
Customer (CustNo, CustFirstName, CustLastName, CustStreet, CustCity, CustState, CustZip, CustBal)
Employee (EmpNo, EmpFirstName, EmpLastName, EmpPhone, EmpEmail, SupEmpNo, EmpCommRate)
Product (ProdNo, ProdName, ProdMfg, ProdQOH, ProdPrice, ProdNextShipDate)
OrderTbl (OrdNo, OrdDate, CustNo, EmpNo, ShpName, ShpStreet, ShpCity, ShpState, ShpZip)
OrdProd (OrdNo, ProdNo, Qty)
#5
Posted 08 October 2008 - 10:54 AM
You've got a good start. The idea behind a join is that you are creating a NEW temporary table.
If you do:
SELECT DISTINCT *
FROM Product
JOIN Product ON OrdProd.ProdNo = Product.ProdNo
JOIN OrdProd ON OrderTbl.OrdNo = OrdProd.OrdNo
You will get a column for EACH column in the original tables. Each record will consist of every possible match between the three original tables. Inner joins require matching data from both tables. Left outer only requires that a record exist for the first (left) table... the right table may not have a match resulting in null records for its result. Right outer is the reverse. Full outer means every record from either table will appear, with a match if possible.
If you do:
SELECT DISTINCT *
FROM Product
JOIN Product ON OrdProd.ProdNo = Product.ProdNo
JOIN OrdProd ON OrderTbl.OrdNo = OrdProd.OrdNo
You will get a column for EACH column in the original tables. Each record will consist of every possible match between the three original tables. Inner joins require matching data from both tables. Left outer only requires that a record exist for the first (left) table... the right table may not have a match resulting in null records for its result. Right outer is the reverse. Full outer means every record from either table will appear, with a match if possible.
#6
Posted 08 October 2008 - 11:39 AM
Ok i would suggest you to read everything on this page
http://www.w3schools.com/sql/default.aspim not making any marketing for anything but this is the right page :D its not a forum or anything it only teachs you MySQL and a lot of cool stuff i've learned here the most i know about mysql
#7
Posted 08 October 2008 - 01:25 PM
+Rep to kresh for a great idea. I strongly recommend w3schools' SQL tutorial as well.


Sign In
Create Account


Back to top









