0% found this document useful (0 votes)
71 views11 pages

SQL Tutorial For Interview: The SQL Select Distinct Statement

This document provides a tutorial on SQL concepts including: - The SELECT DISTINCT statement to return only distinct (different) values from a column. - SQL JOINs such as INNER JOIN, LEFT JOIN, and RIGHT JOIN to combine rows from two or more tables. - Operators like WHERE, BETWEEN, IN, LIKE and wildcards to filter data. - ORDER BY to sort the result set and aliases to rename columns or tables. - FOREIGN KEY constraints to define relationships between tables.

Uploaded by

roysmoss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views11 pages

SQL Tutorial For Interview: The SQL Select Distinct Statement

This document provides a tutorial on SQL concepts including: - The SELECT DISTINCT statement to return only distinct (different) values from a column. - SQL JOINs such as INNER JOIN, LEFT JOIN, and RIGHT JOIN to combine rows from two or more tables. - Operators like WHERE, BETWEEN, IN, LIKE and wildcards to filter data. - ORDER BY to sort the result set and aliases to rename columns or tables. - FOREIGN KEY constraints to define relationships between tables.

Uploaded by

roysmoss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL TUTORIAL FOR INTERVIEW

The SQL SELECT DISTINCT Statement


In a table, some of the columns may contain duplicate values. This is not a problem, however,
sometimes you will want to list only the different (distinct) values in a table.
The DISTINCT eyword can be used to return only distinct (different) values.
SQL SELECT DISTINCT Syntax
S!"!CT DISTINCT column#name(s)
$%&' table#name
SELECT DISTINCT Example
S!"!CT DISTINCT City $%&' (ersons
Operators Allowed in the WHERE Clase
Operator Des!ription
) !*ual
+, Not e*ual
, -reater than
+ "ess than
,) -reater than or e*ual
+) "ess than or e*ual
.!T/!!N .etween an inclusive ran0e
"I1! Search for a pattern
IN If you now the e2act value you want to return for at least one of the columns
The ORDER "# $eyword
itEANz Consultancy Services, an!alore
SQL TUTORIAL FOR INTERVIEW
The &%D!% .3 eyword is used to sort the result4set by a specified column.
The &%D!% .3 eyword sort the records in ascendin0 order by default.
If you want to sort the records in a descendin0 order, you can use the D!SC eyword.
SQL ORDER "# Syntax
S!"!CT column#name(s)
$%&' table name
&%D!% .3 column#name(s) 5SC6D!SC
!75'("!8
S!"!CT 9 $%&' (ersons
&%D!% .3 "astName
The LI$E Operator
The "I1! operator is used to search for a specified pattern in a column.
SQL LI$E Syntax
S!"!CT column#name(s)
$%&' table name
/:!%! column#name "I1! pattern
!75'("! 8
Select the persons livin0 in a city that starts with ;s; from the table above.
S!"!CT 9 $%&' (ersons
/:!%! City "I1! <s=<
Select the persons livin0 in a city that ends with an ;s; from the ;(ersons; table.
S!"!CT 9 $%&' (ersons
/:!%! City "I1! <=s<
Select the persons livin0 in a city that contains the pattern ;tav; from the ;(ersons; table.
S!"!CT 9 $%&' (ersons
/:!%! City "I1! <=tav=<
S>" wildcards can be used when searchin0 for data in a database.
itEANz Consultancy Services, an!alore
SQL TUTORIAL FOR INTERVIEW
SQL Wild!ards
S>" wildcards can substitute for one or more characters when searchin0 for data in a database.
S>" wildcards must be used with the S>" "I1! operator.
/ith S>", the followin0 wildcards can be used8
Wild!ard Des!ription
= 5 substitute for ?ero or more characters
# 5 substitute for e2actly one character
@charlistA 5ny sin0le character in charlist
@BcharlistA
or
@CcharlistA
5ny sin0le character not in charlist
The IN Operator
The IN operator allows you to specify multiple values in a /:!%! clause.
SQL IN Syntax
S!"!CT column#name(s)
$%&' table#name
/:!%! column#name IN (valueD,valueE,...)
Example%
S!"!CT 9 $%&' (ersons
/:!%! "astName IN (<:ansen<,<(ettersen<)
The "ETWEEN Operator
The .!T/!!N operator selects a ran0e of data between two values. The values can be numbers,
te2t, or dates.
SQL "ETWEEN Syntax
S!"!CT column#name(s)
itEANz Consultancy Services, an!alore
SQL TUTORIAL FOR INTERVIEW
$%&' table#name
/:!%! column#name
.!T/!!N valueD 5ND valueE
E&A'(LE %
S!"!CT 9 $%&' (ersons
/:!%! "astName
.!T/!!N <:ansen< 5ND <(ettersen<
SQL Alias
3ou can 0ive a table or a column another name by usin0 an alias. This can be a 0ood thin0 to do
if you have very lon0 or comple2 table names or column names.
5n alias name could be anythin0, but usually it is short.
SQL Alias Syntax )or Ta*les
S!"!CT column#name(s)
$%&' table#name
5S alias#name
SQL Alias Syntax )or Colmns
S!"!CT column#name 5S alias#name
$%&' table#name
Alias Example
S!"!CT po.&rderID, p."astName, p.$irstName
$%&' (ersons 5S p,
(roduct#&rders 5S po
/:!%! p."astName)<:ansen< 5ND p.$irstName)<&la<
The same S!"!CT statement without aliases8
itEANz Consultancy Services, an!alore
SQL TUTORIAL FOR INTERVIEW
S!"!CT (roduct#&rders.&rderID, (ersons."astName, (ersons.$irstName
$%&' (ersons,
(roduct#&rders
/:!%! (ersons."astName)<:ansen< 5ND (ersons.$irstName)<&la<
SQL +OIN
Di))erent SQL +OINs
.efore we continue with e2amples, we will list the types of F&IN you can use, and the
differences between them.
+OIN8 %eturn rows when there is at least one match in both tables
LE,T +OIN8 %eturn all rows from the left table, even if there are no matches in the ri0ht
table
RI-HT +OIN8 %eturn all rows from the ri0ht table, even if there are no matches in the
left table
,.LL +OIN8 %eturn rows when there is a match in one of the tables
SQL INNER +OIN $eyword
The INN!% F&IN eyword return rows when there is at least one match in both tables.
SQL INNER +OIN Syntax
S!"!CT column#name(s)
$%&' table#nameD
INN!% F&IN table#nameE
&N table#nameD.column#name)table#nameE.column#name
(S% INN!% F&IN is the same as F&IN.
SQL INNER +OIN Example
The ;(ersons; table8
(/Id LastName ,irstName Address City
itEANz Consultancy Services, an!alore
SQL TUTORIAL FOR INTERVIEW
D :ansen &la Timoteivn DG Sandnes
E Svendson Tove .or0vn EH Sandnes
H (ettersen 1ari Stor0t EG Stavan0er
The ;&rders; table8
O/Id OrderNo (/Id
D IIJKL H
E MMNIJ H
H EEMLN D
M EMLNE D
L HMINM DL
S!"!CT (ersons."astName, (ersons.$irstName, &rders.&rderNo
$%&' (ersons
INN!% F&IN &rders
&N (ersons.(#Id)&rders.(#Id
&%D!% .3 (ersons."astName
The result4set will loo lie this8
LastName ,irstName OrderNo
:ansen &la EEMLN
:ansen &la EMLNE
itEANz Consultancy Services, an!alore
SQL TUTORIAL FOR INTERVIEW
(ettersen 1ari IIJKL
(ettersen 1ari MMNIJ
The INN!% F&IN eyword return rows when there is at least one match in both tables. If there
are rows in ;(ersons; that do not have matches in ;&rders;, those rows will N&T be listed.
SQL LE,T +OIN $eyword
The "!$T F&IN eyword returns all rows from the left table (table#nameD), even if there are no
matches in the ri0ht table (table#nameE).
SQL LE,T +OIN Syntax
S!"!CT column#name(s)
$%&' table#nameD
"!$T F&IN table#nameE
&N table#nameD.column#name)table#nameE.column#name
(S% In some databases "!$T F&IN is called "!$T &OT!% F&IN.
SQL LE,T +OIN Example
S!"!CT (ersons."astName, (ersons.$irstName, &rders.&rderNo
$%&' (ersons
"!$T F&IN &rders
&N (ersons.(#Id)&rders.(#Id
&%D!% .3 (ersons."astName
The result4set will loo lie this8
LastName ,irstName OrderNo
:ansen &la EEMLN
:ansen &la EMLNE
(ettersen 1ari IIJKL
(ettersen 1ari MMNIJ
Svendson Tove
itEANz Consultancy Services, an!alore
SQL TUTORIAL FOR INTERVIEW
The "!$T F&IN eyword returns all the rows from the left table ((ersons), even if there are no
matches in the ri0ht table (&rders).
SQL RI-HT +OIN $eyword
The %I-:T F&IN eyword %eturn all rows from the ri0ht table (table#nameE), even if there are
no matches in the left table (table#nameD).
SQL RI-HT +OIN Syntax
S!"!CT column#name(s)
$%&' table#nameD
%I-:T F&IN table#nameE
&N table#nameD.column#name)table#nameE.column#name
(S% In some databases %I-:T F&IN is called %I-:T &OT!% F&IN.
SQL RI-HT +OIN Example
S!"!CT (ersons."astName, (ersons.$irstName, &rders.&rderNo
$%&' (ersons
%I-:T F&IN &rders
&N (ersons.(#Id)&rders.(#Id
&%D!% .3 (ersons."astName
The result4set will loo lie this8
LastName ,irstName OrderNo
:ansen &la EEMLN
:ansen &la EMLNE
(ettersen 1ari IIJKL
(ettersen 1ari MMNIJ
HMINM
itEANz Consultancy Services, an!alore
SQL TUTORIAL FOR INTERVIEW
The %I-:T F&IN eyword returns all the rows from the ri0ht table (&rders), even if there are no
matches in the left table ((ersons).
SQL ,.LL +OIN $eyword
The $O"" F&IN eyword return rows when there is a match in one of the tables.
SQL ,.LL +OIN Syntax
S!"!CT column#name(s)
$%&' table#nameD
$O"" F&IN table#nameE
&N table#nameD.column#name)table#nameE.column#name
SQL ,.LL +OIN Example
S!"!CT (ersons."astName, (ersons.$irstName, &rders.&rderNo
$%&' (ersons
$O"" F&IN &rders
&N (ersons.(#Id)&rders.(#Id
&%D!% .3 (ersons."astName
The result4set will loo lie this8
LastName ,irstName OrderNo
:ansen &la EEMLN
:ansen &la EMLNE
(ettersen 1ari IIJKL
(ettersen 1ari MMNIJ
Svendson Tove
HMINM
itEANz Consultancy Services, an!alore
SQL TUTORIAL FOR INTERVIEW
SQL ,OREI-N $E# Constraint
5 $&%!I-N 1!3 in one table points to a (%I'5%3 1!3 in another table.
SQL ,OREI-N $E# Constraint on CREATE TA"LE
The followin0 S>" creates a $&%!I-N 1!3 on the ;(#Id; column when the ;&rders; table is
created8
'ySQL%
C%!5T! T5."! &rders
(
&#Id int N&T NO"",
&rderNo int N&T NO"",
(#Id int,
(%I'5%3 1!3 (&#Id),
$&%!I-N 1!3 ((#Id) %!$!%!NC!S (ersons((#Id)
)
SQL Ser0er 1 Ora!le 1 'S A!!ess%
C%!5T! T5."! &rders
(
&#Id int N&T NO"" (%I'5%3 1!3,
&rderNo int N&T NO"",
(#Id int $&%!I-N 1!3 %!$!%!NC!S (ersons((#Id)
)
To allow namin0 of a $&%!I-N 1!3 constraint, and for definin0 a $&%!I-N 1!3 constraint
on multiple columns, use the followin0 S>" synta28
'ySQL 1 SQL Ser0er 1 Ora!le 1 'S A!!ess%
C%!5T! T5."! &rders
(
&#Id int N&T NO"",
&rderNo int N&T NO"",
(#Id int,
(%I'5%3 1!3 (&#Id),
C&NST%5INT f#(er&rders $&%!I-N 1!3 ((#Id)
%!$!%!NC!S (ersons((#Id)
)
itEANz Consultancy Services, an!alore
SQL TUTORIAL FOR INTERVIEW
SQL ,OREI-N $E# Constraint on ALTER TA"LE
To create a $&%!I-N 1!3 constraint on the ;(#Id; column when the ;&rders; table is already
created, use the followin0 S>"8
'ySQL 1 SQL Ser0er 1 Ora!le 1 'S A!!ess%
5"T!% T5."! &rders
5DD $&%!I-N 1!3 ((#Id)
%!$!%!NC!S (ersons((#Id)
To allow namin0 of a $&%!I-N 1!3 constraint, and for definin0 a $&%!I-N 1!3 constraint
on multiple columns, use the followin0 S>" synta28
To DRO( a ,OREI-N $E# Constraint
SQL Ser0er 1 Ora!le 1 'S A!!ess%
5"T!% T5."! &rders
D%&( C&NST%5INT f#(er&rders
itEANz Consultancy Services, an!alore

You might also like