Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DBMS Lab 6

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Assignment -6

Study and Implementation of


 Group By & having clause
 Order by clause
 Indexing

SQL Group By
The SQL GROUP BY clause is used in collaboration with the SELECT statement to
arrange identical data into groups.
The GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the
ORDER BY clause.

Syntax:
The basic syntax of GROUP BY clause is given below. The GROUP BY clause must follow the
conditions in the WHERE clause and must precede the ORDER BY clause if one is used.

SELECT column1, column2


FROM table_name
WHERE [ conditions ]
GROUP BY column1,
column2 ORDER BY
column1, column2
Example:
Consider the CUSTOMERS table having the following records:

++++++
| ID | NAME| AGE | ADDRESS| SALARY|
++++++
| 1 | Ramesh| 32 | Ahmedabad | 2000.00 |
| 2 | Khilan| 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik| 27 | Bhopal | 8500.00 |
| 6 | Komal| 22 | MP | 4500.00 |
| 7 | Muffy| 24 | Indore | 10000.00 |
++++++

Q.1 Write a query to display NAME, SALARY from CUSTOMER table


group by NAME

The INDEX is used to create and retrieve data from the database very quickly. Index can be
created by using single or group of columns in a table. When index is created, it is assigned a
ROWID for each row before it sorts out the data.

Proper indexes are good for performance in large databases, but you need to be careful while
creating index. Selection of fields depends on what you are using in your SQL queries.

Example:
For example, the following SQL creates a new table called CUSTOMERS and adds five columns:

CREATE TABLE CUSTOMERS(

IDINT NOT NULL,

NAME VARCHAR (20) NOT NULL,

AGE INT NOT NULL,

ADDRESS CHAR (25) , SALARYDECIMAL (18, 2),


PRIMARY KEY (ID)
Now, you can create index on single or multiple columns using the following syntax:

CREATE INDEX index_name

ON table_name ( column1, column2.. . .);

Q. 1 Write a SQL query to create to create an INDEX on AGE


column from CUSTOMER table to optimize the search on customers
for a particular age.

SQL ORDER BY Clause

The SQL ORDER BY clause is used to sort the data in ascending or


descending order, based on one or more columns. Some database sorts query results in
ascending order by default.

Syntax:
The basic syntax of ORDER BY clause is as follows:

SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];

You can use more than one column in the ORDER BY clause. Make sure whatever column
you are using to sort, that column should be in column-list.

Example:
Consider the CUSTOMERS table having the following records:

++++++
| ID | NAME| AGE | ADDRESS| SALARY|
++++++
| 1 | Ramesh| 32 | Ahmedabad | 2000.00 |
| 2 | Khilan| 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik| 27 | Bhopal | 8500.00 |
| 6 | Komal| 22 | MP | 4500.00 |
| 7 | Muffy| 24 | Indore | 10000.00 |
++++++

Following is an example, which would sort the result in ascending order by NAME and SALARY:

SQL> SELECT * FROM CUSTOMERS


ORDER BY NAME, SALARY;
This would produce the following result:

++++++

| ID | NAME | AGE | ADDRESS| SALARY|


++++++
| 4 | Chaitali | 25 | Mumbai| 6500.00 |
| 5 | Hardik| 27 | Bhopal| 8500.00 |
| 3 | kaushik | 23 | Kota| 2000.00 |
| 2 | Khilan| 25 | Delhi| 1500.00 |
| 6 | Komal| 22 | MP| 4500.00 |
| 7 | Muffy| 24 | Indore| 10000.00 |
| 1 | Ramesh| 32 | Ahmedabad | 2000.00 |
++++++

Following is an example, which would sort the result in descending order by NAME:

SQL> SELECT * FROM CUSTOMERS


ORDER BY NAME DESC;

This would produce the following result:

++++++
| ID | NAME| AGE | ADDRESS| SALARY|
++++++
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 24
7 ||Muffy
Indore | 10000.00 |
| 22
6 ||Komal
MP | 4500.00 |
| 25
2 ||Khilan
Delhi | 1500.00 |
| 23
3 ||kaushik
Kota | 2000.00 |
| 27
5 ||Hardik
Bhopal | 8500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
++++++

Q.1 Perform all ORDER BY clause to sort Customer table in Ascending and descending
order.

You might also like