Vous êtes sur la page 1sur 14

SQL SELECT

The SQL SELECT statement is used to select data from a SQL database table. This is usually the very first SQL command every SQL newbie learns and this is because the SELECT SQL statement is one of the most used SQL commands. Please have a look at the general SQL SELECT syntax SELECT Column!" Column#" Column$" %&'( Table! The list of column names after the SQL SELECT command determines which columns you want to be returned in your result set. )f you want to select all columns from a database table" you can use the following SQL statement SELECT * %&'( Table! +hen the list of columns following the SELECT SQL command is re,laced with asterix -*. all table columns are returned. +ord of caution here" it/s always better to ex,licitly s,ecify the columns in the SELECT list" as this will im,rove your 0uery ,erformance significantly. The table name following the SQL %&'( keyword -in our case Table!. tells the SQL inter,reter which table to use to retrieve the data.

SQL SELECT INTO


The SQL SELECT INTO statement is used to select data from a SQL database table and to insert it to a different table at the same time. The general SQL SELECT INTO syntax looks like this SELECT Column!" Column#" Column$" )1T' Table# %&'( Table! The list of column names after the SQL SELECT command determines which columns will be co,ied" and the table name after the SQL )1T' keyword s,ecifies to which table to co,y those rows. )f we want to make an exact co,y of the data in our Customers table" we need the following SQL SELECT INTO statement SELECT * )1T' Customers2co,y %&'( Customers

SQL DISTINCT
The SQL DISTINCT clause is used together with the SQL SELECT keyword" to return a dataset with uni0ue entries for certain database table column. +e will use our Customers database table to illustrate the usage of SQL DISTINCT. FirstName 3ohn Steven Paula 3ames LastName Smith ;oldfish >rown Smith Email 3ohn.Smith4yahoo.com goldfish4fishhere.net ,b4herowndomain.org ?im4su,ergig.co.uk DOB #565!789 6565!7<6 =5#65!7<9 #@5!@5!79@ Phone 8#8 ###:#### $#$ 6==:6=6= 6!8 $#$:$#$# 6!8 $#$:9999

%or exam,le if we want to select all distinct surnames from our Customers table" we will use the following SQL DISTINCT statement SELECT A)ST)1CT Last1ame %&'( Customers The result of the SQL DISTINCT ex,ression above will look like this LastName Smith ;oldfish

>rown

SQL WHERE
The SQL WHERE clause is used to select data conditionally" by adding it to already existing SQL SELECT 0uery. +e are going to use the Customers table from the ,revious cha,ter" to illustrate the use of the SQL WHERE command. Table Customers FirstName 3ohn Steven Paula 3ames LastName Smith ;oldfish >rown Smith Email 3ohn.Smith4yahoo.com goldfish4fishhere.net ,b4herowndomain.org ?im4su,ergig.co.uk DOB #565!789 6565!7<6 =5#65!7<9 #@5!@5!79@ Phone 8#8 ###:#### $#$ 6==:6=6= 6!8 $#$:$#$# 6!8 $#$:9999

)f we want to select all customers from our database table" having last name BSmithB we need to use the following SQL syntax SELECT * %&'( Customers +CE&E Last1ame D BSmithB The result of the SQL ex,ression above will be the following FirstName 3ohn 3ames LastName Smith Smith Email 3ohn.Smith4yahoo.com ?im4su,ergig.co.uk DOB #565!789 #@5!@5!79@ Phone 8#8 ###:#### 6!8 $#$:9999

)n this sim,le SQL 0uery we used the EDE -E0ual. o,erator in our +CE&E criteria Last1ame D BSmithB >ut we can use any of the following com,arison o,erators in con?unction with the SQL WHERE clause <> Not E!ual" SELECT * %&'( Customers +CE&E Last1ame FG BSmithB > #reater than" SELECT * %&'( Customers +CE&E A'> G B!5!5!7<@B >$ #reater or E!ual" SELECT * %&'( Customers +CE&E A'> GD B!5!5!7<@B < Less than" SELECT * %&'( Customers +CE&E A'> F B!5!5!7<@B <$ Less or E!ual" SELECT * %&'( Customers +CE&E A'> DF B!5!5!7<@B LI%E similar to" SELECT * %&'( Customers +CE&E Phone L)HE B8#8IB 1ote the L)HE syntax is different with the different &A>(S -SQL Server syntax used above.. Check the SQL L)HE article for more details. Bet&een De'ines a ran(e" SELECT * %&'( Customers +CE&E A'> >ET+EE1 B!5!5!7<@B J1A B!5!5!7<=B

SQL LI%E
+e will use the Customers table to illustrate the SQL LI%E clause usage FirstName 3ohn Steven Paula 3ames LastName Smith ;oldfish >rown Smith Email 3ohn.Smith4yahoo.com goldfish4fishhere.net ,b4herowndomain.org ?im4su,ergig.co.uk DOB #565!789 6565!7<6 =5#65!7<9 #@5!@5!79@ Phone 8#8 ###:#### $#$ 6==:6=6= 6!8 $#$:$#$# 6!8 $#$:9999

The SQL LI%E clause is very useful when you want to s,ecify a search condition within your SQL +CE&E clause" based on a ,art of a column contents. %or exam,le if you want to select all customers having %irst1ame starting with B3B you need to use the following SQL statement SELECT * %&'( Customers +CE&E %irst1ame L)HE B3IB Cere is the result of the SQL statement above FirstName 3ohn 3ames LastName Smith Smith Email 3ohn.Smith4yahoo.com ?im4su,ergig.co.uk DOB #565!789 #@5!@5!79@ Phone 8#8 ###:#### 6!8 $#$:9999

)f you want to select all Customers with ,hone numbers starting with B6!8B you will use this SQL ex,ression SELECT * %&'( Customers +CE&E Phone L)HE B6!8IB The BIB is a so called wildcard character and re,resents any string in our ,attern. Kou can ,ut the wildcard anywhere in the string following the SQL LI%E clause and you can ,ut as many wildcards as you like too. 1ote that different databases use different characters as wildcard characters" for exam,le BIB is a wildcard character for (S SQL Server re,resenting any string" and B*B is the corres,onding wildcard character used in (S Jccess. Jnother wildcard character is B2B re,resenting any single character. The BLMB s,ecifies a range of characters. Cave a look at the following SQL statement SELECT * %&'( Customers +CE&E Phone L)HE BL6:8M28IB This SQL ex,ression will return all customers satisfying the following conditions

The Phone column starts with a digit between 6 and 8 -L6:8M. Second character in the Phone column can be anything -2. The third character in the Phone column is 8 -8. The remainder of the Phone column can be any character string -I.
Cere is the result of this SQL ex,ression FirstName 3ohn Paula 3ames LastName Smith >rown Smith Email 3ohn.Smith4yahoo.com ,b4herowndomain.org ?im4su,ergig.co.uk DOB #565!789 =5#65!7<9 #@5!@5!79@ Phone 8#8 ###:#### 6!8 $#$:$#$# 6!8 $#$:9999

SQL INSERT INTO


The SQL INSERT INTO syntax has # main forms and the result of either of them is adding a new row into the database table.

The first syntax form of the )1SE&T )1T' SQL clause doesnBt s,ecify the column names where the data will be inserted" but ?ust their values )1SE&T )1T' Table! NJLOES -value!" value#" value$P. The second form of the SQL INSERT INTO command" s,ecifies both the columns and the values to be inserted in them )1SE&T )1T' Table! -Column!" Column#" Column$P. NJLOES -Nalue!" Nalue#" Nalue$P. Js you might already have guessed" the number of the columns in the second )1SE&T )1T' syntax form must match the number of values into the SQL statement" otherwise you will get an error. )f we want to insert a new row into our Customers table" we are going to use one of the following # SQL statements )1SE&T )1T' Customers NJLOES -BPeterB" BCuntB" B,eter.hunt4tgmail.netB" B!5!5!7<6B" B8#8 999:9999B. )1SE&T )1T' Customers -%irst1ame" Last1ame" Email" A'>" Phone. NJLOES -BPeterB" BCuntB" B,eter.hunt4tgmail.netB" B!5!5!7<6B" B8#8 999:9999B. The result of the execution of either of the # )1SE&T )1T' SQL statements will be a new row added to our Customers database table FirstName 3ohn Steven Paula 3ames Peter LastName Smith ;oldfish >rown Smith Cunt Email 3ohn.Smith4yahoo.com goldfish4fishhere.net ,b4herowndomain.org ?im4su,ergig.co.uk ,eter.hunt4tgmail.net DOB #565!789 6565!7<6 =5#65!7<9 #@5!@5!79@ !5!5!7<6 Phone 8#8 ###:#### $#$ 6==:6=6= 6!8 $#$:$#$# 6!8 $#$:9999 8#8 999:9999

)f you want to enter data for ?ust a few of the table columns" you/ll have to use the second syntax form of the SQL INSERT INTO clause" because the first form will ,roduce an error if you haven/t su,,lied values for all columns. To insert only the %irst1ame and Last1ame columns" execute the following SQL statement )1SE&T )1T' Customers -%irst1ame" Last1ame. NJLOES -BPeterB" BCuntB.

SQL )PD*TE
The SQL )PD*TE general syntax looks like this OPAJTE Table! SET Column! D Nalue!" Column# D Nalue# +CE&E Some2Column D Some2Nalue The SQL )PD*TE clause changes the data in already existing database row-s. and usually we need to add a conditional SQL +CE&E clause to our SQL )PD*TE statement in order to s,ecify which row-s. we intend to u,date. )f we want to u,date the (r. Steven ;oldfishBs date of birth to B=5!@5!7<6B in our Customers database table FirstName 3ohn Steven Paula 3ames LastName Smith ;oldfish >rown Smith Email 3ohn.Smith4yahoo.com goldfish4fishhere.net ,b4herowndomain.org ?im4su,ergig.co.uk DOB #565!789 6565!7<6 =5#65!7<9 #@5!@5!79@ Phone 8#8 ###:#### $#$ 6==:6=6= 6!8 $#$:$#$# 6!8 $#$:9999

we need the following SQL )PD*TE statement OPAJTE Customers SET A'> D B=5!@5!7<6B +CE&E Last1ame D B;oldfishB J1A %irst1ame D BStevenB

)f we don/t s,ecify a +CE&E clause in the SQL ex,ression above" all customersB A'> will be u,dated to B=5!@5!7<6B" so be careful with the SQL )PD*TE command usage. +e can u,date several database table rows at once" by using the SQL +CE&E clause in our OPAJTE statement. %or exam,le if we want to change the ,hone number for all customers with last name Smith -we have # in our exam,le Customers table." we need to use the following SQL )PD*TE statement OPAJTE Customers SET Phone D B8#8 ===:====B +CE&E Last1ame D BSmithB Jfter the execution of the OPAJTE SQL ex,ression above" the Customers table will look as follows FirstName 3ohn Steven Paula 3ames LastName Smith ;oldfish >rown Smith Email 3ohn.Smith4yahoo.com goldfish4fishhere.net ,b4herowndomain.org ?im4su,ergig.co.uk DOB #565!789 6565!7<6 =5#65!7<9 #@5!@5!79@ Phone 8#8 ===:==== $#$ 6==:6=6= 6!8 $#$:$#$# 8#8 ===:====

SQL DELETE
So far we/ve learnt how to select data from a database table and how to insert and u,date data into a database table. 1ow it/s time to learn how to remove data from a database. Cere comes the SQL DELETE statementQ The SQL DELETE command has the following generic SQL syntax AELETE %&'( Table! +CE&E Some2Column D Some2Nalue )f you ski, the SQL +CE&E clause when executing SQL DELETE ex,ression" then all the data in the s,ecified table will be deleted. The following SQL statement will delete all the data from our Customers table and we/ll end u, with com,letely em,ty table AELETE %&'( Table! )f you s,ecify a +CE&E clause in your SQL DELETE statement" only the table rows satisfying the +CE&E criteria will be deleted AELETE %&'( Customers +CE&E Last1ame D BSmithB The SQL 0uery above will delete all database rows having Last1ame BSmithB and will leave the Customers table in the following state FirstName Steven Paula LastName ;oldfish >rown Email goldfish4fishhere.net ,b4herowndomain.org DOB 6565!7<6 =5#65!7<9 Phone $#$ 6==:6=6= 6!8 $#$:$#$#

SQL ORDER B+
The SQL ORDER B+ clause comes in handy when you want to sort your SQL result sets by some column-s.. %or exam,le if you want to select all the ,ersons from the already familiar Customers table and order the result by date of birth" you will use the following statement SELECT * %&'( Customers '&AE& >K A'> The result of the above SQL ex,ression will be the following FirstName 3ohn Steven LastName Smith ;oldfish Email 3ohn.Smith4yahoo.com goldfish4fishhere.net DOB #565!789 6565!7<6 Phone 8#8 ###:#### $#$ 6==:6=6=

Paula 3ames

>rown Smith

,b4herowndomain.org ?im4su,ergig.co.uk

=5#65!7<9 #@5!@5!79@

6!8 $#$:$#$# 6!8 $#$:9999

Js you can see the rows are sorted in ascending order by the A'> column" but what if you want to sort them in descending orderR To do that you will have to add the AESC SQL keyword after your SQL ORDER B+ clause SELECT * %&'( Customers '&AE& >K A'> AESC The result of the SQL 0uery above will look like this FirstName 3ames Paula Steven 3ohn LastName Smith >rown ;oldfish Smith Email ?im4su,ergig.co.uk ,b4herowndomain.org goldfish4fishhere.net 3ohn.Smith4yahoo.com DOB #@5!@5!79@ =5#65!7<9 6565!7<6 #565!789 Phone 6!8 $#$:9999 6!8 $#$:$#$# $#$ 6==:6=6= 8#8 ###:####

)f you donBt s,ecify how to order your rows" al,habetically or reverse" than the result set is ordered al,habetically" hence the following to SQL ex,ressions ,roduce the same result SELECT * %&'( Customers '&AE& >K A'> SELECT * %&'( Customers '&AE& >K A'> JSC Kou can sort your result set by more than one column by s,ecifying those columns in the SQL ORDER B+ list. The following SQL ex,ression will order by A'> and Last1ame SELECT * %&'( Customers '&AE& >K A'>" Last1ame

SQL *ND , OR
The SQL *ND clause is used when you want to s,ecify more than one condition in your SQL +CE&E clause" and at the same time you want all conditions to be true. %or exam,le if you want to select all customers with %irst1ame E3ohnE and Last1ame ESmithE" you will use the following SQL ex,ression SELECT * %&'( Customers +CE&E %irst1ame D B3ohnB J1A Last1ame D BSmithB The result of the SQL 0uery above is FirstName 3ohn LastName Smith Email 3ohn.Smith4yahoo.com DOB #565!789 Phone 8#8 ###:####

The following row in our Customer table" satisfies the second of the conditions -Last1ame D BSmithB." but not the first one -%irst1ame D B3ohnB." and thatBs why itBs not returned by our SQL 0uery FirstName 3ames LastName Smith Email ?im4su,ergig.co.uk DOB #@5!@5!79@ Phone 6!8 $#$:9999

The SQL OR statement is used in similar fashion and the ma?or difference com,ared to the SQL J1A is that '& clause will return all rows satisfying any of the conditions listed in the +CE&E clause. )f we want to select all customers having %irst1ame B3amesB or %irst1ame BPaulaB we need to use the following SQL statement SELECT * %&'( Customers +CE&E %irst1ame D B3amesB '& %irst1ame D BPaulaB The result of this 0uery will be the following FirstName LastName Email DOB Phone

Paula 3ames

>rown Smith

,b4herowndomain.org ?im4su,ergig.co.uk

=5#65!7<9 #@5!@5!79@

6!8 $#$:$#$# 6!8 $#$:9999

Kou can combine J1A and '& clauses anyway you want and you can use ,arentheses to define your logical ex,ressions. Cere is an exam,le of such a SQL 0uery" selecting all customers with Last1ame B>rownB and %irst1ame either B3amesB or BPaulaB SELECT * %&'( Customers +CE&E -%irst1ame D B3amesB '& %irst1ame D BPaulaB. J1A Last1ame D B>rownB The result of the SQL ex,ression above will be FirstName Paula LastName >rown Email ,b4herowndomain.org DOB =5#65!7<9 Phone 6!8 $#$:$#$#

SQL IN
The SQL IN clause allows you to s,ecify discrete values in your SQL +CE&E search criteria. TCE SQL IN syntax looks like this SELECT Column!" Column#" Column$" P %&'( Table! +CE&E Column! )1 -Nalu!" Nalue#" P. Lets use the Em,loyeeCours table to illustrate how SQL IN works Em-lo.ee 3ohn Smith Jllan >abel Tina Crown 3ohn Smith Jllan >abel Tina Crown 3ohn Smith Jllan >abel Tina Crown Date =585#@@6 =585#@@6 =585#@@6 =5<5#@@6 =5<5#@@6 =5<5#@@6 =595#@@6 =595#@@6 =595#@@6 Hours 9 9 9 7 9 !@ 9 9 7

Consider the following SQL 0uery using the SQL IN clause SELECT * %&'( Em,loyeeCours +CE&E Aate )1 -B=585#@@6B" B=5<5#@@6B. This SQL ex,ression will select only the entries where the column Aate has value of B=585#@@6B or B=5<5#@@6B" and you can see the result below Em-lo.ee 3ohn Smith Jllan >abel Tina Crown 3ohn Smith Jllan >abel Tina Crown Date =585#@@6 =585#@@6 =585#@@6 =5<5#@@6 =5<5#@@6 =5<5#@@6 Hours 9 9 9 7 9 !@

+e can use the SQL IN statement with another column in our Em,loyeeCours table

SELECT * %&'( Em,loyeeCours +CE&E Cours )1 -7" !@. The result of the SQL 0uery above will be Em-lo.ee 3ohn Smith Tina Crown Tina Crown Date =5<5#@@6 =5<5#@@6 =595#@@6 Hours 7 !@ 7

SQL BETWEEN
The SQL BETWEEN , *ND keywords define a range of data between # values. The SQL BETWEEN syntax looks like this SELECT Column!" Column#" Column$" P %&'( Table! +CE&E Column! >ET+EE1 Nalue! J1A Nalue# The # values defining the range for SQL BETWEEN clause can be dates" numbers or ?ust text. )n contrast with the SQL )1 keyword" which allows you to s,ecify discrete values in your SQL +CE&E criteria" the SQL BETWEEN gives you the ability to s,ecify a range in your search criteria. +e are going to use the familiar Customers table to show how SQL BETWEEN works FirstName 3ohn Steven Paula 3ames LastName Smith ;oldfish >rown Smith Email 3ohn.Smith4yahoo.com goldfish4fishhere.net ,b4herowndomain.org ?im4su,ergig.co.uk DOB #565!789 6565!7<6 =5#65!7<9 #@5!@5!79@ Phone 8#8 ###:#### $#$ 6==:6=6= 6!8 $#$:$#$# 6!8 $#$:9999

Consider the following SQL BETWEEN statement SELECT * %&'( Customers +CE&E A'> >ET+EE1 B!5!5!7<=B J1A B!5!5#@@6B The SQL BETWEEN statement above will select all Customers having A'> column between B!5!5!7<=B and B!5!5#@@6B dates. Cere is the result of this SQL ex,ression FirstName Paula 3ames LastName >rown Smith Email ,b4herowndomain.org ?im4su,ergig.co.uk DOB =5#65!7<9 #@5!@5!79@ Phone 6!8 $#$:$#$# 6!8 $#$:9999

SQL *LI*SES
SQL aliases can be used with database tables and with database table columns" de,ending on task you are ,erforming. SQL column aliases are used to make the out,ut of your SQL 0ueries easy to read and more meaningful SELECT Em,loyee" SO(-Cours. Js SumCoursPerEm,loyee %&'( Em,loyeeCours ;&'OP >K Em,loyee )n the exam,le above we created SQL alias SumCoursPerEm,loyee and the result of this SQL 0uery will be the following Em-lo.ee 3ohn Smith Jllan >abel Tina Crown SumHoursPerEm-lo.ee #= #6 #<

Consider the following SQL statement" showing how to use SQL table aliases SELECT Em,.Em,loyee %&'( Em,loyeeCours JS Em, Cere is the result of the SQL ex,ression above Em-lo.ee 3ohn Smith Jllan >abel Tina Crown The SQL table aliases are very useful when you select data from multi,le tables.

SQL CO)NT
The SQL CO)NT aggregate function is used to count the number of rows in a database table. The SQL CO)NT syntax is sim,le and looks like this SELECT C'O1T-Column!. %&'( Table! )f we want to count the number of customers in our Customers table" we will use the following SQL CO)NT statement SELECT C'O1T-Last1ame. JS 1umber'fCustomers %&'( Customers The result of this SQL CO)NT 0uery will be Num/erO'Customers 6

SQL 0*1
The SQL 0*1 aggregate function allows us to select the highest -maximum. value for a certain column. The SQL 0*1 function syntax is very sim,le and it looks like this SELECT (JS-Column!. %&'( Table! )f we use the Customers table from our ,revious cha,ters" we can select the highest date of birth with the following SQL 0*1 ex,ression SELECT (JS-A'>. JS (axA'> %&'( Customers

SQL 0IN
The SQL 0IN aggregate function allows us to select the lowest -minimum. value for a certain column. The SQL 0IN function syntax is very sim,le and it looks like this SELECT ()1-Column!. %&'( Table! )f we use the Customers table from our ,revious cha,ters" we can select the lowest date of birth with the following SQL 0IN ex,ression SELECT ()1-A'>. JS (inA'> %&'( Customers

SQL *2#

The SQL *2# aggregate function selects the average value for certain table column. Cave a look at the SQL *2# syntax SELECT JN;-Column!. %&'( Table! )f we want to find out what is the average SaleJmount in the Sales table" we will use the following SQL *2# statement SELECT JN;-SaleJmount. JS JvgSaleJmount %&'( Sales which will result in the following dataset *3(Sale*mount T!7=.<$

SQL S)0
The SQL S)0 aggregate function allows selecting the total for a numeric column. The SQL S)0 syntax is dis,layed below SELECT SO(-Column!. %&'( Table! +e are going to use the Sales table to illustrate the use of SQL S)0 clause Sales4 CustomerID # ! $ $ 6 Consider the following SQL S)0 statement SELECT SO(-SaleJmount. %&'( Sales This SQL statement will return the sum of all SaleJmount fields and the result of it will be Sale*mount T7<9.8< 'f course you can s,ecify search criteria using the SQL +CE&E clause in your SQL S)0 statement. )f you want to select the total sales for customer with Customer)A D $" you will use the following SQL S)0 statement SELECT SO(-SaleJmount. %&'( Sales +CE&E Customer)A D $ The result will be Sale*mount T###.7= Date =585#@@6 =5<5#@@6 =5<5#@@6 =5!$5#@@6 =5##5#@@6 Sale*mount T!@@.## T77.7= T!##.7= T!@@.@@ T===.==

SQL #RO)P B+
The SQL #RO)P B+ statement is used along with the SQL aggregate functions like SO( to ,rovide means of grou,ing the result dataset by certain database table column-s.. The best way to ex,lain how and when to use the SQL #RO)P B+ statement is by exam,le" and that/s what we are going to do.

Consider the following database table called Em,loyeeCours storing the daily hours for each em,loyee of a factious com,any Em-lo.ee 3ohn Smith Jllan >abel Tina Crown 3ohn Smith Jllan >abel Tina Crown 3ohn Smith Jllan >abel Tina Crown Date =585#@@6 =585#@@6 =585#@@6 =5<5#@@6 =5<5#@@6 =5<5#@@6 =595#@@6 =595#@@6 =595#@@6 Hours 9 9 9 7 9 !@ 9 9 7

)f the manager of the com,any wants to get the sim,le sum of all hours worked by all em,loyees" he needs to execute the following SQL statement SELECT SO( -Cours. %&'( Em,loyeeCours >ut what if the manager wants to get the sum of all hours for each of his em,loyeesR To do that he need to modify his SQL 0uery and use the SQL #RO)P B+ statement SELECT Em,loyee" SO( -Cours. %&'( Em,loyeeCours ;&'OP >K Em,loyee The result of the SQL ex,ression above will be the following Em-lo.ee 3ohn Smith Jllan >abel Tina Crown Hours #= #6 #<

Js you can see we have only one entry for each em,loyee" because we are grou,ing by the Em,loyee column. The SQL #RO)P B+ clause can be used with other SQL aggregate functions" for exam,le SQL JN; SELECT Em,loyee" JN;-Cours. %&'( Em,loyeeCours ;&'OP >K Em,loyee The result of the SQL statement above will be Em-lo.ee 3ohn Smith Jllan >abel Tina Crown Hours 9.$$ 9 7

)n our Em,loyee table we can grou, by the date column too" to find out what is the total number of hours worked on each of the dates into the table SELECT Aate" SO(-Cours. %&'( Em,loyeeCours ;&'OP >K Aate Cere is the result of the above SQL ex,ression Date =585#@@6 Hours #6

=5<5#@@6 =595#@@6

#< #=

SQL H*2IN#
The SQL H*2IN# clause is used to restrict conditionally the out,ut of a SQL statement" by a SQL aggregate function used in your SELECT list of columns. Kou canBt s,ecify criteria in a SQL +CE&E clause against a column in the SELECT list for which SQL aggregate function is used. %or exam,le the following SQL statement will generate an error SELECT Em,loyee" SO( -Cours. %&'( Em,loyeeCours +CE&E SO( -Cours. G #6 ;&'OP >K Em,loyee The SQL H*2IN# clause is used to do exactly this" to s,ecify a condition for an aggregate function which is used in your 0uery SELECT Em,loyee" SO( -Cours. %&'( Em,loyeeCours ;&'OP >K Em,loyee CJN)1; SO( -Cours. G #6 The above SQL statement will select all em,loyees and the sum of their res,ective hours" as long as this sum is greater than #6. The result of the SQL H*2IN# clause can be seen below Em-lo.ee 3ohn Smith Tina Crown Hours #= #<

SQL 5OIN
The SQL 5OIN clause is used whenever we have to select data from # or more tables. To be able to use SQL 5OIN clause to extract data from # -or more. tables" we need a relationshi, between certain columns in these tables. +e are going to illustrate our SQL 5OIN exam,le with the following # tables Customers4 CustomerID ! # $ 6 Sales4 CustomerID # ! $ $ 6 Date =585#@@6 =5<5#@@6 =5<5#@@6 =5!$5#@@6 =5##5#@@6 Sale*mount T!@@.## T77.7= T!##.7= T!@@.@@ T===.== FirstName 3ohn Steven Paula 3ames LastName Smith ;oldfish >rown Smith Email 3ohn.Smith4yahoo.com goldfish4fishhere.net ,b4herowndomain.org ?im4su,ergig.co.uk DOB #565!789 6565!7<6 =5#65!7<9 #@5!@5!79@ Phone 8#8 ###:#### $#$ 6==:6=6= 6!8 $#$:$#$# 6!8 $#$:9999

Js you can see those # tables have common field called Customer)A and thanks to that we can extract information from both tables by matching their Customer)A columns. Consider the following SQL statement SELECT Customers.%irst1ame" Customers.Last1ame" SO(-Sales.SaleJmount. JS SalesPerCustomer %&'( Customers" Sales +CE&E Customers.Customer)A D Sales.Customer)A ;&'OP >K Customers.%irst1ame" Customers.Last1ame The SQL ex,ression above will select all distinct customers -their first and last names. and the total res,ective amount of dollars they have s,ent. The SQL 5OIN condition has been s,ecified after the SQL +CE&E clause and says that the # tables have to be matched by their res,ective Customer)A columns. Cere is the result of this SQL statement FirstName 3ohn Steven Paula 3ames LastName Smith ;oldfish >rown Smith SalesPerCustomers T77.7= T!@@.## T###.7= T===.==

The SQL statement above can be re:written using the SQL 5OIN clause like this SELECT Customers.%irst1ame" Customers.Last1ame" SO(-Sales.SaleJmount. JS SalesPerCustomer %&'( Customers 3')1 Sales '1 Customers.Customer)A D Sales.Customer)A ;&'OP >K Customers.%irst1ame" Customers.Last1ame There are # ty,es of SQL 3')1S U INNER 5OINS and O)TER 5OINS. )f you donBt ,ut )11E& or 'OTE& keywords in front of the SQL 5OIN keyword" then INNER 5OIN is used. )n short E)11E& 3')1E D E3')1E -note that different databases have different syntax for their 3')1 clauses.. The INNER 5OIN will select all rows from both tables as long as there is a match between the columns we are matching on. )n case we have a customer in the Customers table" which still hasnBt made any orders -there are no entries for this customer in the Sales table." this customer will not be listed in the result of our SQL 0uery above. )f the Sales table has the following rows CustomerID # ! Date =585#@@6 =585#@@6 Sale*mount T!@@.## T77.7=

Jnd we use the same SQL 5OIN statement from above SELECT Customers.%irst1ame" Customers.Last1ame" SO(-Sales.SaleJmount. JS SalesPerCustomer %&'( Customers 3')1 Sales '1 Customers.Customer)A D Sales.Customer)A ;&'OP >K Customers.%irst1ame" Customers.Last1ame +eBll get the following result FirstName 3ohn Steven LastName Smith ;oldfish SalesPerCustomers T77.7= T!@@.##

Even though Paula and 3ames are listed as customers in the Customers table they wonBt be dis,layed because they havenBt ,urchased anything yet. >ut what if you want to dis,lay all the customers and their sales" no matter if they have ordered something or notR +e/ll do that with the hel, of SQL O)TER 5OIN clause. The second ty,e of SQL 5OIN is called SQL O)TER 5OIN and it has # sub:ty,es called LEFT O)TER 5OIN and RI#HT O)TER 5OIN.

The LEFT O)TER 5OIN or sim,ly LEFT 5OIN -you can omit the 'OTE& keyword in most databases." selects all the rows from the first table listed after the %&'( clause" no matter if they have matches in the second table. )f we slightly modify our last SQL statement to SELECT Customers.%irst1ame" Customers.Last1ame" SO(-Sales.SaleJmount. JS SalesPerCustomer %&'( Customers LE%T 3')1 Sales '1 Customers.Customer)A D Sales.Customer)A ;&'OP >K Customers.%irst1ame" Customers.Last1ame and the Sales table still has the following rows CustomerID # ! The result will be the following FirstName 3ohn Steven Paula 3ames LastName Smith ;oldfish >rown Smith SalesPerCustomers T77.7= T!@@.## 1OLL 1OLL Date =585#@@6 =585#@@6 Sale*mount T!@@.## T77.7=

Js you can see we have selected everything from the Customers -first table.. %or all rows from Customers" which don/t have a match in the Sales -second table." the SalesPerCustomer column has amount 1OLL -1OLL means a column contains nothing.. The RI#HT O)TER 5OIN or ?ust RI#HT 5OIN behaves exactly as SQL LEFT 5OIN" exce,t that it returns all rows from the second table -the right table in our SQL 5OIN statement..

Vous aimerez peut-être aussi