0% found this document useful (0 votes)
43 views8 pages

RDBMS Assignment

The document discusses views, indexes, and aggregate functions in SQL. It provides the syntax and examples for creating views with the CREATE VIEW statement and dropping views with DROP VIEW. It also covers creating indexes with CREATE INDEX and dropping indexes with DROP INDEX. Finally, it details the most common aggregate functions - COUNT, SUM, AVG, MIN, and MAX - and provides the syntax and examples for using each function.

Uploaded by

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

RDBMS Assignment

The document discusses views, indexes, and aggregate functions in SQL. It provides the syntax and examples for creating views with the CREATE VIEW statement and dropping views with DROP VIEW. It also covers creating indexes with CREATE INDEX and dropping indexes with DROP INDEX. Finally, it details the most common aggregate functions - COUNT, SUM, AVG, MIN, and MAX - and provides the syntax and examples for using each function.

Uploaded by

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

TITLE: Execute SQL Queries for Views and Index

1)VIEW :

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.

CREATE VIEW Syntax


CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example
CREATE VIEW [ Pune Customers] AS
SELECT CustomerName,
FROM Customers
WHERE City = 'Pune';

SQL DROP VIEW Syntax


DROP VIEW view_name;

The following SQL drops the "Brazil Customers" view:

Example

DROP VIEW [Pune Customers];

2)INDEX

Index statement is used to create indexes in tables.

Indexes are used to retrieve data from the database more quickly than
otherwise.

The users cannot see the indexes, they are just used to speed up
searches/queries.
CREATE INDEX Syntax

Creates an index on a table. Duplicate values are allowed:

CREATE INDEX index_name


ON table_name (column1, column2, ...)

Example
CREATE INDEX idx_lastname
ON Custemers (LastName);

DROP INDEX Syntax


DROP INDEX index_name ON table_name;

Example
DROP INDEX idx_lastname;

Create Table Customers

CustomerNo FirstName LastName City


TILE: EXECUTE SQL queries for
1. Date functions with all formats.
2. Time functions with all-time formats.

1)Date functions with all formats.

Function Description

GETDATE() Returns the current date and time

DATEPART() Returns a single part of a date/time

DATEADD() Adds or subtracts a specified time interval from a date

DATEDIFF() Returns the time between two dates

CONVERT() Displays date/time data in different formats

SQL Server comes with the following data types for storing a date or a
date/time value in the database:

 DATE - format YYYY-MM-DD


 DATETIME - format: YYYY-MM-DD HH:MI:SS
 SMALLDATETIME - format: YYYY-MM-DD HH:MI:SS
 TIMESTAMP - format: a unique number

OrderId ProductName OrderDate


1 Coffee Tables 2008-11-11

2 Study Tables 2008-11-09

3 Chairs 2008-11-11

4 Beds 2008-10-29

Create Table Orders

Now we want to select the records with an OrderDate of "2008-11-11" from


the table above.

We use the following SELECT statement:

SELECT * FROM Orders WHERE OrderDate='2008-11-11'

The result-set will look like this:

OrderId ProductName OrderDate

1 Coffee Tables 2008-11-11

3 Chairs 2008-11-11
2) Time functions with all-time formats.

Syntax
TIME_FORMAT(time, format)

Format Description

%f Microseconds (000000 to 999999)

%H Hour (00 to 23)

%h Hour (00 to 12)

%I Hour (00 to 12)

%i Minutes (00 to 59)

%p AM or PM

%r Time in 12 hour AM or PM format (hh:mm:ss AM/PM)

%S Seconds (00 to 59)

%s Seconds (00 to 59)

%T Time in 24 hour format (hh:mm:ss)


Example
SELECT TIME_FORMAT("19:30:10", "%H %i %s");

The TIME() function extracts the time part from a given time/datetime.

This function returns "00:00:00" if expression is not a datetime/time, or


NULL if expression is NULL.

Syntax
TIME(expression)

expression Required. The time/datetime to extract the time from

Example

Extract the time part from the datetime expression:

SELECT TIME("2022-11-10 11:30:10");

Extract the time part from NULL:

SELECT TIME(NULL);
Title : Execute SQL queries for all aggregate functions.
Aggregate Function in SQL:-An aggregate function in SQL returns one value after calculating
multiple values of a column.

Various types of SQL aggregate functions are:

 Count()

 Sum()

 Avg()

 Min()

 Max()

1) COUNT() Function

The COUNT() function returns the number of rows in a database table.

Syntax: COUNT( [ALL|DISTINCT] expression )

Example: SELECT COUNT(ProductID) FROM Products;

2) Sum() Function

The SUM() function returns the total sum of a numeric column.

Syntax: SUM( [ALL|DISTINCT] expression

Example: SELECT SUM(Price) FROM Products;

3) AVG() function

The AVG() function calculates the average of a set of values.

Syntax: AVG( [ALL|DISTINCT] expression )

Example: SELECT AVG(Price) FROM Products;


4) MIN() function

The MIN() aggregate function returns the lowest value (minimum) in a set of non-NULL
values.

Syntax: MIN( [ALL|DISTINCT] expression )

Example: SELECT MIN(Price) FROM Products;

5) MAX() function

The MAX() aggregate function returns the highest value (maximum) in a set of non-NULL
values.

Syntax: AVG( [ALL|DISTINCT] expression )

Example: SELECT MIN(Price) FROM Products;

Conclusion

The aggregate function in SQL is very powerful in the database. In this assignment , we
have seen several examples of aggregate functions.

You might also like