You are on page 1of 7

Database Management System (SQL)

1) NOW()

The NOW () function returns the current system date and time.
Syntax: SELECT NOW () FROM table_name
For example:

We have the following "Products" table:

Prod_Id ProductName Unit UnitPrice


1 Kissan Jam 1000 g 10.45
2 Oral B Toothbrush 2000 g 32.56
3 Ponds Powder 1500 g 15.67

Now we want to display the products and prices per today's date.

We use the following SELECT statement:

SELECT ProductName, UnitPrice, Now() as PerDate FROM Products

The result-set will look like this:

ProductName UnitPrice PerDate


Kissan Jam 10.45 21/4/2010 11:25:02 AM
Oral B Toothbrush 32.56 21/4/2010 11:25:02 AM
Ponds Powder 15.67 21/4/2010 11:25:02 AM

2) FORMAT ()
The FORMAT () function is used to format how a field is to be displayed.

Syntax:

SELECT FORMAT(column_name,format) FROM table_name

Parameter Description
column_name Required. The field to be formatted.
format Required. Specifies the format.
For Example:

We have the following "Products" table:

Prod_Id ProductName Unit UnitPrice


1 Kissan Jam 1000 g 10.45
2 Oral B Toothbrush 1000 g 32.56
3 Ponds Powder 1000 g 15.67

Now we want to display the products and prices per today's date (with today's date displayed in the
following format "YYYY-MM-DD").

We use the following SELECT statement:

SELECT ProductName, UnitPrice, FORMAT(Now(),'YYYY-MM-DD') as PerDate


FROM Products

The result-set will look like this:

ProductName UnitPrice PerDate


Kissan Jam 10.45 2010-04-21
Oral B Toothbrush 32.56 2010-04-21
Ponds Powder 15.67 2010-04-21

3) ROUND ()

The ROUND () function is used to round a numeric field to the number of decimals specified.

Syntax: SELECT ROUND (column_name,decimals) FROM table_name

Parameter Description
column_name Required. The field to round.
decimals Required. Specifies the number of decimals to be returned.

For example:

We have the following "Products" table:

Prod_Id ProductName Unit UnitPrice


1 Kissan Jam 1000 g 10.45
2 Oral B Toothbrush 1000 g 32.56
3 Ponds Powder 1000 g 15.67
Now we want to display the product name and the price rounded to the nearest integer.

We use the following SELECT statement:

SELECT ProductName, ROUND(UnitPrice,0) as UnitPrice FROM Products

The result-set will look like this:

ProductName UnitPrice
Kissan Jam 10
Oral B Toothbrush 33
Ponds Powder 16

4) Views ()

 In SQL, a view is a virtual table based on the result-set of an SQL statement.

 A view contains rows and columns, just like a real table. The fields in a view are fields from
one or more real tables in the database.

 You can add SQL functions, WHERE, and JOIN statements to a view and present the data as
if the data were coming from one single table.

SQL CREATE VIEW Syntax

CREATE VIEW view_name AS


SELECT column_name(s)
FROM table_name
WHERE condition

SQL Updating a View

You can update a view by using the following syntax:

SQL CREATE OR REPLACE VIEW Syntax

CREATE OR REPLACE VIEW view_name AS


SELECT column_name(s)
FROM table_name
WHERE condition
SQL Dropping a View

You can delete a view with the DROP VIEW command.

SQL DROP VIEW Syntax

DROP VIEW view_name

5) Outer join

An outer join does not require each record in the two joined tables to have a matching record.
The joined table retains each record—even if no other matching record exists. Outer joins
subdivide further into left outer joins, right outer joins, and full outer joins, depending on which
table(s) one retains the rows from (left, right, or both).

Left Outer Join

Use this when you only want to return rows that have matching data in the left table, even if there's no
matching rows in the right table.

Example SQL statement

SELECT * FROM Individual AS Ind


LEFT JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId

Source Tables

Left Table

Id FirstName LastName UserName


1 Fred Flinstone freddo
2 Homer Simpson homey
3 Homer Brown notsofamous
4 Ozzy Ozzbourne sabbath
5 Homer Gain noplacelike
Right Table

IndividualId AccessLevel
1 Administrator
2 Contributor
3 Contributor
4 Contributor
10 Administrator

Result

IndividualId FirstName LastName UserName IndividualId AccessLevel


1 Fred Flinstone freddo 1 Administrator
2 Homer Simpson homey 2 Contributor
3 Homer Brown notsofamous 3 Contributor
4 Ozzy Osbourne sabbath 4 Contributor
5 Homer Gain noplacelike NULL NULL

Right Outer Join

Use this when you only want to return rows that have matching data in the right table, even if there's no
matching rows in the left table.

Example SQL statement

SELECT * FROM Individual AS Ind


RIGHT JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId

Source Tables

Left Table

Id FirstName LastName UserName


1 Fred Flinstone freddo
2 Homer Simpson homey
3 Homer Brown notsofamous
4 Ozzy Ozzbourne sabbath
5 Homer Gain noplacelike
Right Table

IndividualId AccessLevel
1 Administrator
2 Contributor
3 Contributor
4 Contributor
10 Administrator

Result

IndividualId FirstName LastName UserName IndividualId AccessLevel


1 Fred Flinstone freddo 1 Administrator
2 Homer Simpson homey 2 Contributor
3 Homer Brown notsofamous 3 Contributor
4 Ozzy Osbourne sabbath 4 Contributor
NULL NULL NULL NULL 10 Administrator

Full Outer Join

Use this when you want to all rows, even if there's no matching rows in the right table.

Example SQL statement

SELECT * FROM Individual AS Ind


FULL JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId

Source Tables

Left Table

Id FirstName LastName UserName


1 Fred Flinstone freddo
2 Homer Simpson homey
3 Homer Brown notsofamous
4 Ozzy Ozzbourne sabbath
5 Homer Gain noplacelike
Right Table

IndividualId AccessLevel
1 Administrator
2 Contributor
3 Contributor
4 Contributor
10 Administrator

Result

IndividualId FirstName LastName UserName IndividualId AccessLevel


1 Fred Flinstone freddo 1 Administrator
2 Homer Simpson homey 2 Contributor
3 Homer Brown notsofamous 3 Contributor
4 Ozzy Osbourne sabbath 4 Contributor
5 Homer Gain noplacelike NULL NULL
NULL NULL NULL NULL 10 Administrator

You might also like