Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
28Activity
0 of .
Results for:
No results containing your search query
P. 1
SQL Commands

SQL Commands

Ratings: (0)|Views: 4,844 |Likes:
Published by simply_coool
SQL Commands
SQL Commands

More info:

Published by: simply_coool on Aug 12, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

04/23/2013

pdf

text

original

 
Basics of the SELECT Statement
In a relational database, data is stored in tables. An example table would relate Social Security Number, Name, andAddress:
EmployeeAddressTableSSNFirstNameLastNameAddressCityState
512687458JoeSmith83 First StreetHowardOhio758420012MaryScott842 Vine Ave.LosantivilleOhio102254896SamJones33 Elm St.ParisNew Yor876512563SarahAckerman440 U.S. 110UptonMichigan
Query 1
let’s say you want to see the address of each employee. Use the SELECT statement, like so:
Conditional Selection
To further discuss the SELECT statement, let's look at a new example table (for hypothetical purposes only):
EmployeeStatisticsTableEmployeeIDNoSalaryBenefitsPosition
0107500015000Manage1056500015000Manage1526000015000Manage2156000012500Manage2445000012000Staf3004500010000Staf3354000010000Staf400320007500Entry-Level441280007500Entry-Level
Relational Operators
There are six Relational Operators in SQL, and after introducing them, we'll see how they're used:=Equal< or != (seemanual) Not Equal<Less Than>Greater Than<=Less Than or Equal To>=Greater Than or Equal ToThe
WHERE 
clause is used to specify that only certain rows of the table are displayed, based on the criteria describedin that
WHERE clause
. It is most easily understood by looking at a couple of examples.
Query 2
EMPLOYEEIDNO's of those making at or over $50,000
 
The
 AND
operator joins two or more conditions, and displays a row only if that row's data satisfies
ALL
conditionslisted (i.e. all conditions hold true).
Query 3
To display all staff making over $40,000, use:The
OR
operator joins two or more conditions, but returns a row if 
ANY
of the conditions listed hold true.
Query 4
To see all those who make less than $40,000 or have less than $10,000 in benefits, listed together, use the followingquery:AND & OR can be combined, for example:
Query 5
Find all managers whose salary is greater than 60000 and benefits are greater than 12000
 IN & BETWEEN 
An easier method of using compound conditions uses
 IN 
or 
 BETWEEN.
Query 6
For example, if you wanted to list all managers and staff:
Query 7
To list those making greater than or equal to $30,000, but less than or equal to $50,000, use:
Query 8
To list everyone not in this range, try:
Using
LIKE 
Query 9
Look at the EmployeeStatisticsTable, and say you wanted to see all people whose last names started with "S"; try:
Joins
In this section, we will only discuss
inner 
joins, and
equijoins
, as in general, they are the most useful. For moreinformation, try the SQL links at the bottom of the page.Good database design suggests that each table lists data only about a single
entity
, and detailed information can beobtained in a relational database, by using additional tables, and by using a
 join
.
 
AntiqueOwnersOwnerIDOwnerLastNameOwnerFirstName
01JonesBill02SmithBob15LawsonPatricia21AkinsJane50FowlerSam
OrdersOwnerIDItemDesired
02Table02Des21Chai15Mirro
AntiquesSellerIDBuyerIDItem
0150Bed0215Table1502Chai2150Mirro5001Des0121Cabinet0221Coffee Table1550Chai0115Jewelry Box0221Pottery2102Bookcase5001Plant Stand
Keys
First, let's discuss the concept of 
keys
. A
 primary key
is a column or set of columns that uniquely identifies the rest of the data in any given row. For example, in the AntiqueOwners table, the OwnerID column uniquely identifies that row.This means two things: no two rows can have the same OwnerID, and, even if two owners have the same first and lastnames, the OwnerID column ensures that the two owners will not be confused with each other, because the uniqueOwnerID column will be used throughout the database to track the owners, rather than the names.A
foreign key
is a column in a table where that column is a primary key of another table, which means that any data in aforeign key column must have corresponding data in the other table where that column is the primary key. In DBMS-speak, this correspondence is known as
referential integrity
. For example, in the Antiques table, both the BuyerID andSellerID are foreign keys to the primary key of the AntiqueOwners table (OwnerID; for purposes of argument, one hasto be an Antique Owner before one can buy or sell any items), as, in both tables, the ID rows are used to identify theowners or buyers and sellers, and that the OwnerID is the primary key of the AntiqueOwners table. In other words, all

Activity (28)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
sangho,woo liked this
vijay306 liked this
syriluit liked this
Ashi Sharma liked this
Mohammad Alam liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->