Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Newbie to SQL..Is this correct?

Author  Topic 

Set_0ne
Starting Member

5 Posts

Posted - 2004-03-25 : 07:34:39
Hi, I've just started to learn SQL since 6 hours ago. I have to write an SQL statement. I'm learning it for school but it's correspondance and responces are slow.

I've already writen up my code..I just want to know if it is correct or not. I'll also put up the question to show it all.And if there is any mistakes can someone correct it..I would be very greatful :)

This is there question:
Question: Retreive all records from the 'students' table where the field 'enrolled_course'contains "database development". The statement must order the results alphanumerically by the country field.

Here is what I wrote:
SELECT students FROM enrolled_course WHERE all_records= "database_development" ORDER by country field

Thankyou

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2004-03-25 : 07:44:36
Getting there but not quite. You have most of the right bits, but placed in the wrong order.

The cool thing about SQL is that it can be very close to English.

You have been asked to retrieve records FROM the students table WHERE enrolled_course contains that phrase. Read it out loud then think about your SQL statment again.

As for the "contains" bit, look in Books Online (the SQL Server documentation) or a book for the LIKE condition.



Damian
Go to Top of Page

Set_0ne
Starting Member

5 Posts

Posted - 2004-03-25 : 07:55:37
Okay, thanks. I will try it out again. So, you mean I have it right like 'SELECT students' but it shouldn't be placed at the start..maybe placed else where?
Go to Top of Page

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2004-03-25 : 08:00:48
No, select goes at the beginning because it's a select statement.

But because you want to select data FROM the students table, the word students should go some place else. Say the original question out loud and it should make some sense.


Damian
Go to Top of Page

Set_0ne
Starting Member

5 Posts

Posted - 2004-03-25 : 09:39:54
Okay, I've been thinking about it and I know I haven't gotten it fully yet but I think I'm on the right track..

SELECT all_records FROM students_table WHERE students =database_development
And this is where I get lost..

I'm taking a stab at where students should go tho.
Go to Top of Page

sphadke
Yak Posting Veteran

55 Posts

Posted - 2004-03-25 : 11:49:51
Go to Books online (BOL) and do a search on SELECT , WHERE and you should get your answer.


Sachin
Go to Top of Page

jsiedliski
Yak Posting Veteran

61 Posts

Posted - 2004-03-25 : 19:24:09
Seems like the gurus are trying to guide you and not just give you the correct answer. I respect that!

A wise man once said. "If I bring you a fish, you will eat today, but if I teach you to fish, then you will never go hungry."

Anyway, just wanted to add 2 cents here. 10 years ago, when I was learning SQL in school, my Professor told me to always remember the basic construct:

SELECT (what you want to retreive)
FROM (where the information is - in other words the table names)
WHERE (if you want some of the records based on some criteria, specify it here - example: where state = 'CA')

Good Luck

:)
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2004-03-25 : 19:35:32
Since you are so new to this, you may not be familiar with Books Online (typically abbreviated as BOL). You should have BOL in your Microsoft SQL Server program group, or you can shortcut to it by hitting the 'F1' key while using Query Analyzer.

BOL is arguably the best documentation Microsoft has ever produced. Take Damian's advice and look up SELECT in BOL for more info. BOL is also full of example SQL statements, the section on SELECT has a dozen or so to help you get familiar.

Also, don't forget your ORDER BY clause.


-ec
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-03-25 : 19:39:44
Another really good book that you can find for almost nothing on Amazon is Sams Teach yourself Transact-SQL in 21 days. If you just are learning how to write SQL, it's easy to read, has good examples, and can get you up and writing good SQL fast.

Hopefully, it will be enough to make you really want to learn the advanced features of SQL Server.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

Set_0ne
Starting Member

5 Posts

Posted - 2004-03-26 : 02:59:52
Okay, thinking about it some more is this correct:
select * from students where enrolled_course = 'database_development' order by county
or
SELECT * FROM students WHERE enrolled_course LIKE 'database development' ORDER BY country_field
or
SELECT * FROM students WHERE enrolled_course LIKE 'database development%' ORDER BY country_field

And thanks for your information everyone :)
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2004-03-26 : 07:14:54
very close....to be 100% accurate to satisfy the contains bit...you need 2 %...1 at the start and one at the end....and you don't need the _field

SELECT * FROM students WHERE enrolled_course LIKE 'database development%' ORDER BY country_field


Also one of the best other resources you can get is our own "www.sqlteam.com"....browse other peoples code snippets posted here and try to figure out what is going on....the variety/complexity/simplicity/absurdity will astound you. It'll also give you a good idea as to some good programming styles....and give practical approaches to solving many types of problem.
Go to Top of Page
   

- Advertisement -