User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 422,651 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,643 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums

Basic SQL made Hard

Join Date: Feb 2005
Location: Ballarat, Australia
Posts: 164
Reputation: Paul.Esson is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 6
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Basic SQL made Hard

  #1  
Feb 15th, 2005
Intro

I'm not the best at this, but I thought we were lacking a SQL tutorial...


Test table

My Little Test Table called "test" for this great exercise contains the following

FirstName LastName EyeColor
Paul Esson Brown
John Smith Blue
John Howard Green
Kim Beazley Gray


SELECT

This selects data from a table in SQL. The basic syntax of select is as follows:

SELECT field FROM table

So
SELECT * FROM test

will produce the table an output of everything in the test table

SELECT FirstName FROM test

Will Produce

Paul
John
John
Kim

As you can clearly see, this part is quite easy.


WHERE

If we don't want to know about two johns we can try

SELECT DISTINCT FirstName FROM test

This Will Produce

Paul
John
Kim

John Smith is the John Selected, since John Howard is after him in the database.

SELECT LastName FROM test WHERE FirstName = "John" 

Will Produce

Smith
Howard

WHERE can be used with lots of different logical operators, such as
= is equal to
<> or != is not equal to
> greater than
< less than
>= less than or equal to
<= ....

IN A list follows this, if its in the list then its used
such as
SELECT FirstName FROM test WHERE LastName IN ("Esson", "Smith")
NOT IN ........

BETWEEN Value is used if its between two values
SELECT ItemName FROM stockTable WHERE Quantity BETWEEN 100 AND 50
NOT BETWEEN ......

IS NULL Where the Field is Null
IS NOT NULL ......

LIKE Where the field is like an expression _ represents
one char % represents multiple

SELECT FirstName FROM test WHERE FirstName LIKE "P_ul"

This will output Paul
NOT LIKE ........

Now lets order our table by FirstName

SELECT * FROM test ORDER BY FirstName

and we get our table but ordered by FirstName


INSERT

INSERT INTO table (field1, field2, etc....) VALUES (value1, value2, etc...)

That was quick!

AND

Yet again I do not want to look at Howard another easy way of forgeting about him is to use and

SELECT FirstName,LastName FROM test WHERE FirstName = "John" AND LastName != "Howard"

This Produces John Smith

Now ends Pauls basic SQL made hard tutorial...
Last edited by happygeek : Oct 28th, 2006 at 9:19 am. Reason: Formatting
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 3:52 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC