23

06.01 sql select distinct

Embed Size (px)

Citation preview

Page 1: 06.01 sql select distinct
Page 2: 06.01 sql select distinct

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 keyword can be used to return

only distinct (different) values.

SQL SELECT DISTINCT Syntax

SELECT DISTINCT column_name(s)

FROM table_name

Page 3: 06.01 sql select distinct

SELECT DISTINCT Person.PName

FROM Person;

Id Name Address Hobby

1123 Anita Damauli stamps

1123 Anita Damauli coins

5556 BinodKathmand

u hiking

9876 BarshaKathmand

u stamps

PName

Anita

Barsha

Binod

Page 4: 06.01 sql select distinct

SQL AND & OR Operators

The AND & OR operators are used to filter

records based on more than one condition

The AND operator displays a record if both the

first condition and the second condition are

true.

The OR operator displays a record if either the

first condition or the second condition is true.

Page 5: 06.01 sql select distinct

SQL OR Operators

SELECT * FROM Person

WHERE Person.Hobby='hiking' OR

Person.ID>3000;

ID PName Address Hobby

5556 Binod Kathmandu hiking

9876 Barsha Kathmandu stamps

Page 6: 06.01 sql select distinct

SQL AND Operators

SELECT *

FROM Person

WHERE Person.ID>3000 AND Person.ID<5999;

ID PName Address Hobby

5556 Binod Kathmandu hiking

Page 7: 06.01 sql select distinct

SQL ORDER BY

The ORDER BY keyword is used to sort the

result-set.

The ORDER BY keyword is used to sort the

result-set by a specified column.

The ORDER BY keyword sorts the records in

ascending order by default.

If you want to sort the records in a descending

order, you can use the DESC keyword.

Page 8: 06.01 sql select distinct

SQL ORDER BY Syntax

SELECT column_name(s)

FROM table_name

ORDER BY column_name(s) ASC|DESC

SELECT Person.Hobby

FROM Person

ORDER BY Person.Hobby;

Hobby

coins

hiking

stamps

stamps

Page 9: 06.01 sql select distinct

SQL INSERT INTO

The INSERT INTO statement is used to insert a new row in a table.

SQL INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two forms.

The first form doesn't specify the column names where the data will be inserted, only their values:INSERT INTO table_name VALUES (value1, value2,

value3,...)

The second form specifies both the column names and the values to be inserted:INSERT INTO table_name (column1, column2, column3,...)

VALUES (value1, value2, value3,...)

Page 10: 06.01 sql select distinct

SQL INSERT INTO

INSERT INTO Persons

VALUES (4,'Nilsen', 'Johan', 'Bakken 2',

'Stavanger')

INSERT INTO Persons (P_Id, LastName,

FirstName)

VALUES (5, 'Tjessem', 'Jakob')

Page 11: 06.01 sql select distinct

SQL UPDATE

The UPDATE statement is used to update existing records in a table.

SQL UPDATE Syntax:

UPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_value

Example:

UPDATE PersonsSET Address=‘Nayabazar', City=‘Kathmandu'WHERE LastName=‘Ghimire' AND FirstName=‘Bishal'

Page 12: 06.01 sql select distinct

SQL UPDATE Warning

Be careful when updating records. If we had

omitted the WHERE clause in the example

above, like this:

UPDATE Persons

SET Address=‘Nayabazar', City=‘Kathmandu‘

What will be the result ?

Page 13: 06.01 sql select distinct

SQL DELETE

The DELETE statement is used to delete rows

in a table.

SQL DELETE Syntax

DELETE FROM table_name

WHERE some_column=some_value

DELETE FROM Persons

WHERE Address=‘Nayabazar', City=‘Kathmandu‘

Page 14: 06.01 sql select distinct

SQL TOP

The TOP clause is used to specify the number of records to return.

The TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.

Note: Not all database systems support the TOP clause.

SQL Server Syntax

SELECT TOP number|percent column_name(s)FROM table_name

Page 15: 06.01 sql select distinct

SQL TOP

MySQL Syntax

SELECT column_name(s)FROM table_nameLIMIT number

Oracle Syntax

SELECT column_name(s)FROM table_nameWHERE ROWNUM <= number

SELECT * FROM Persons LIMIT 5

Page 16: 06.01 sql select distinct

SQL LIKE

The LIKE operator is used to search for a

specified pattern in a column.

SQL LIKE Syntax

SELECT column_name(s)

FROM table_name

WHERE column_name LIKE pattern

Page 17: 06.01 sql select distinct

SQL LIKE

To select the persons living in a city that starts

with "s" from the table.

SELECT * FROM Persons

WHERE City LIKE ‘k%'

Page 18: 06.01 sql select distinct

SQL Wildcards

Wildcard Description

% A substitute for zero or more characters

_ A substitute for exactly one character

[charlist] Any single character in charlist

[^charlist]or

[!charlist]

Any single character not in charlist

• SQL wildcards can substitute for one or more

characters when searching for data in a

database.

• SQL wildcards must be used with the SQL

LIKE operator.

Page 19: 06.01 sql select distinct

SQL IN

The IN operator allows you to specify multiple

values in a WHERE clause.

SQL IN Syntax

SELECT column_name(s)

FROM table_name

WHERE column_name IN (value1,value2,...)

Page 20: 06.01 sql select distinct

Example #1

Select *

From Address

Where FirstName IN ('Mary', 'Sam')

Example #2

SELECT *

FROM Address

WHERE FirstName = 'Mary'

OR FirstName = 'Sam'

Page 21: 06.01 sql select distinct

SQL BETWEEN

The BETWEEN operator selects a range of

data between two values. The values can be

numbers, text, or dates.

SQL BETWEEN Syntax

SELECT column_name(s)

FROM table_name

WHERE column_name

BETWEEN value1 AND value2

Page 22: 06.01 sql select distinct

Example:

SELECT * FROM suppliers WHERE supplier_id

BETWEEN 5000 AND 5010;

SELECT * FROM suppliers WHERE supplier_id

>= 5000 AND supplier_id <= 5010;

Page 23: 06.01 sql select distinct

example:

SELECT * FROM orders WHERE order_date

between to_date ('2013/01/01', 'yyyy/mm/dd')

AND to_date ('2013/12/31', 'yyyy/mm/dd');

SELECT * FROM orders WHERE order_date >=

to_date('2013/01/01', 'yyyy/mm/dd') AND

order_date <=

to_date('2013/12/31','yyyy/mm/dd');