Basic SQL question (1 Viewer)

damien

Well-Known Member
Since 2000
Joined
Nov 19, 2000
Messages
5,726
Location
D8
Just gone back to school. Doing computer stuff for the first time and finding it a little difficult.

Here's what I need to do:

List all OrderIDs for orders whose totalAmount is between €250 and €500.

Here's what I've done:

SELECT orderid
FROM order
WHERE totalamount IN(SELECT totalamount
FROM orderid
WHERE totalamountBETWEEN 250.00 AND 500.00);

Am I right?
 
Is totalamount a field of order? I think it should be enough just to have :

select orderid from order where totalamount>250 and totalamount<500
 
SELECT orderid
FROM orders
WHERE totalamount BETWEEN 250 AND 500

Tip: You SELECT columns FROM tables. Separate out the query in the extraneous IN statement and you can see it's not possible to use "FROM orderid". (orderid is a column, not a table).

I see the rationale behind your query. To check if something is within a range, just use BETWEEN. Use IN if you want to check for multiple values i.e.

SELECT orderid
FROM order
WHERE totalamount IN(300,350,400); (300/350/400 = totalamount).

This would only return those orderids whose total amounts are either 300, 350 or 400.
 
Thanks guys. Selecting from orderid was a typo (or a brain typo), I know that you select from tables.

The reason for my subquery was that I thought that I thought that the column I used in my WHERE statement had to be one I'd already SELECTed.. but obviously not.

This shit is tough. Having major Java issues as well. How did youse all manage to twist your brain around this stuff? Or am I just thick?
 
Thanks guys. Selecting from orderid was a typo (or a brain typo), I know that you select from tables.

The reason for my subquery was that I thought that I thought that the column I used in my WHERE statement had to be one I'd already SELECTed.. but obviously not.

This shit is tough. Having major Java issues as well. How did youse all manage to twist your brain around this stuff? Or am I just thick?

I did all this jazz about 12 years ago. Really enjoyed the puzzle-like element to it but there were definitely people in the class who 'got it' quicker than others. I found SQL fairly intuitive - never did java. There are tons of resources online for all of this stuff.
 
Some stuff is definitely more intuitive than others. SQL isn't too bad. Java is a different kettle of fish. Hopefully it'll all come together in time.
 
Once you start speaking java in your head it gets a lot easier but the first while til you crack it is pretty mind boggling. Gotta think of the computer of a really simple person who needs explaining stuff in a simple language in great detail.
 
Thanks guys. Selecting from orderid was a typo (or a brain typo), I know that you select from tables.

The reason for my subquery was that I thought that I thought that the column I used in my WHERE statement had to be one I'd already SELECTed.. but obviously not.

This shit is tough. Having major Java issues as well. How did youse all manage to twist your brain around this stuff? Or am I just thick?

I like to think of the 'where' part as the filtering condition and the 'select' part as the projection, the fields that you want to pull out into your result. SQL is tough alright. Especially when you get onto subqueries and multiple joins and all that complex relational algebra stuff. That stuff can make your head hurt.
 

Users who are viewing this thread

Activity
So far there's no one here
Old Thread: Hello . There have been no replies in this thread for 365 days.
Content in this thread may no longer be relevant.
Perhaps it would be better to start a new thread instead.

21 Day Calendar

Mohammad Syfkhan 'I Am Kurdish' Dublin Album Launch
Bello Bar
1 Portobello Harbour, Saint Kevin's, Dublin, Ireland
Mohammad Syfkhan 'I Am Kurdish' Dublin Album Launch
Bello Bar
1 Portobello Harbour, Saint Kevin's, Dublin, Ireland
Gig For Gaza w/ ØXN, Junior Brother, Pretty Happy & Mohammad Syfkhan
Vicar Street
58-59 Thomas St, The Liberties, Dublin 8, Ireland
Bloody Head, Hubert Selby Jr Infants, Creepy Future - Dublin
Anseo
18 Camden Street Lower, Saint Kevin's, Dublin, Ireland

Support thumped.com

Support thumped.com and upgrade your account

Upgrade your account now to disable all ads... If we had any... Which we don't right now.

Upgrade now

Latest threads

Latest Activity

Loading…
Back
Top