More on SQL Select Statements

by top54u.com 06 Nov, 2007

 

Frequent problem of Database Management is How to prevent duplicate records from the retrieved result when you call a SQL select statement.

 

SQL Query Example to eliminate duplicate results:
First SQL query to get all rows including duplicates:

 

use pubs  -- this statement is used to select pubs database of SQL Server 2000

select stor_id from sales

Results:

  stor_id
1 6380
2 8042
3 8042
4 7896
5 7896
6 7896
7 7131
8 8042
9 8042
10 7066

 

SQL query excluding duplicate records using DISTINCT keyword:

select DISTINCT stor_id from sales

Results:

  stor_id
1 6380
2 7066
3 7067
4 7131
5 7896
6 8042

 

Row Selection (using WHERE clause) and Search Conditions

WHERE clause is used to select the records those pass the search condition. SQL query includes the following records when search condition is applied:

  • If search condition returns true the records is included into the result.

  • If search condition returns false then the record is excluded from the result.

  • NULL values are also excluded from the result.

 

Five Basic Search Conditions:

  1. Comparison Test: Compares the record with specified comparison operators such as =, >, <, <>, <= and >= to filter them accordingly.

  2. NULL Value Test: It checks the value of column whether it is NULL or not.

  3. Set Member Test: Compare the retrieved value with set members and includes it in the results if it passes the condition e.g. {10, 20, 30, 40}.

  4. Range Test: Checks whether the returned value falls between the specified ranges e.g. BETWEEN 1000 and 3000.

  5. Pattern Matching: Pattern matching test works on string data and compares the retrieved string whether it starts with letter ‘S’ or its second character is ‘a’.

 

Comparison Test

(= operator) returns only those rows where right hand side value matched the column value.
Example:
Select * from sales where qty=5

Result:

  stor_id ord_num ord_date qty payterms title_id
1 6380 6871 9/14/1994 0:00 5 Net 60 BU1032

 

(> operator) returns only those rows where column value is greater than right hand side value. (Excluding right hand side value)
Example:
Select * from sales where qty>5

Result:

  stor_id ord_num ord_date qty payterms title_id
1 7066 A2976 00:00.0 50 Net 30 PC8888
2 7066 QA7442.3 00:00.0 75 ON invoice PS2091
3 7067 D4482 00:00.0 10 Net 60 PS2091
4 7067 P2121 00:00.0 40 Net 30 TC3218
5 7067 P2121 00:00.0 20 Net 30 TC4203

 

(< operator) returns those results where column value is less than right hand side value. (Excluding right hand side value)
Example:
Select * from sales where qty < 10

Result:

  stor_id ord_num ord_date qty payterms title_id

1

6380 6871 00:00.0 5 Net 60 BU1032

2

6380 722a 00:00.0 3 Net 60 PS2091

 

(<= operator) returns results having value less than right hand side value but including right hand side value

(>= operator) returns results having value greater than right hand side value but including right hand side value

(<> operator) works as not equal to operator. It returns results having value not equal to right hand side value e.g.
Select * from sales where qty <> 10

 

NULL Value Test
This test determines whether a given value is NULL or not.
Example:
Select * from sales where title_id IS NULL

Or

Select * from sales where title_id IS NOT NULL

 

Set Member Test
Member Test checks for the resultant value whether it exists in the set or not.
Example:
Select * from sales where qty in (3, 5, 10)

Result:

  stor_id ord_num ord_date qty payterms title_id
1 6380 6871 00:00.0 5 Net 60 BU1032
2 6380 722a 00:00.0 3 Net 60 PS2091
3 7067 D4482 00:00.0 10 Net 60 PS2091
4 7896 TQ456 00:00.0 10 Net 60 MC2222
5 8042 423LL930 00:00.0 10 ON invoice BU1032

 

Range Test
It compares the retrieved value if it exists between the specified ranges. BETWEEN keyword is used along with AND to specify the ranges which is equivalent to (>=) AND (=<) range.
Example:
Select * from sales where qty BETWEEN 20 AND 50

Result:

  stor_id ord_num ord_date qty payterms title_id
1 7067 P2121 00:00.0 40 Net 30 TC3218
2 7067 P2121 00:00.0 20 Net 30 TC4203
3 7067 P2121 00:00.0 20 Net 30 TC7777
4 7131 N914008 00:00.0 20 Net 30 PS2091
5 7131 N914014 00:00.0 25 Net 30 MC3021
6 7131 P3087a 00:00.0 20 Net 60 PS1372
7 7131 P3087a 00:00.0 25 Net 60 PS2106
8 7131 P3087a 00:00.0 25 Net 60 PS7777
9 7896 X999 00:00.0 35 ON invoice BU2075
10 8042 P723 00:00.0 25 Net 30 BU1111
11 8042 QA879.1 00:00.0 30 Net 30 PC1035

 

 

Pattern Matching Test
Pattern matching is done by using LIKE keyword along with wildcard characters:

  1.  ‘%’ percentage symbol is used to match the string.

  2. ‘_’ underscore symbol is used to match the individual character at the position where ‘_’ is placed.


 

Example: Returns names starting with “P” letter.

use pubs
select fname from employee where fname like 'P%'

Result:

  fname
1 Patricia
2 Palle
3 Peter
4 Paolo
5 Pirkko
6 Pedro
7 Paula
8 Philip
9 Paul

 

Example:
Select fname from employee where fname like 'Pau_'

Result:

  fname
1 Paul

 

Example: returns names starting with any character having 2nd & 3rd characters “au” ending with any string.

Select fname from employee where fname like '_au%'

Results:

  fname
1 Laurence
2 Paula
3 Paul

 

Pattern Matching with character search range
[] braces
are used to specify characters or ranges of characters.

 

Example:
Select fname from employee where fname like '[h-j]%'

Result:

  fname
1 Helen
2 Helvetius
3 Howard
4 Janine

 

The table shows the use of wildcards enclosed in square brackets.

Symbol Meaning
LIKE '5[%]' 5%
LIKE '5%' 5 followed by any string of 0 or more characters
LIKE '[_]n' _n
LIKE '_n' an, in, on (and so on)
LIKE '[a-cdf]' a, b, c, d, or f
LIKE '[-acdf]' -, a, c, d, or f
LIKE '[ [ ]' [
LIKE ']' ]

Also learn to CREATE database table and perform SQL Insert-Update-Delete Commands

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:36:17 AM




related videos.....
recent posts.....
top54u ezines.....