0% found this document useful (0 votes)
31 views5 pages

Dbms

The document discusses various SQL commands like sorting, aggregation, joins etc. It contains sample tables Employee and Student. It shows examples of sorting data using ORDER BY on multiple columns. Aggregation is demonstrated using GROUP BY to count rows grouped by subject and year. HAVING clause is used to filter groups. Different types of joins - inner, left and right joins are exhibited on the tables to retrieve matching and non-matching rows.

Uploaded by

TarunVashishth
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)
31 views5 pages

Dbms

The document discusses various SQL commands like sorting, aggregation, joins etc. It contains sample tables Employee and Student. It shows examples of sorting data using ORDER BY on multiple columns. Aggregation is demonstrated using GROUP BY to count rows grouped by subject and year. HAVING clause is used to filter groups. Different types of joins - inner, left and right joins are exhibited on the tables to retrieve matching and non-matching rows.

Uploaded by

TarunVashishth
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/ 5

Table : Employee Table : Student

Sno Name salary age subject Year Name

------------------------------------------- ----------------------------------

1 Harsh 20000 23 English 2 Saraswat

2 Dhanraj 20000 22 English 1 Sachin

3 Ashish 20000 25 Maths 2 Durga

4 Harsh 26000 27 Hindi 1 Shyam

5 Ben 24000 22 Hindi 1 jimmy

(c). Sorting commands :

Syntax : SELECT * FROM table_name ORDER BY column1 ASC|DESC , column2


ASC|DESC

Example : SELECT * FROM Student ORDER BY year ASC , name DESC;

Output :

subject Year Name

---------------------------------------

Hindi 1 Shyam

English 1 Sachin

Hindi 1 jimmy

English 2 Saraswat

Maths 2 Durga

(d). Aggregating the data using group by :


Syntax :

SELECT column1, function_name(column2)

FROM table_name

WHERE condition

GROUP BY column1, column2

ORDER BY column1, column2

Example :

SELECT SUBJECT, YEAR, Count(*) FROM Student GROUP BY SUBJECT,YEAR;

Output :

SUBJECT YEAR Count(*)

-----------------------------------------

English 1 1

English 2 1

Hindi 1 2

• Use of Having Clause :

SELECT column1, function_name(column2)

FROM table_name

WHERE condition

GROUP BY column1, column2

HAVING condition

ORDER BY column1, column2;

Example :(1) SELECT NAME, SUM(SALARY) FROM Employee


GROUP BY NAME HAVING SUM(SALARY)>20000;

OUTPUT :

NAME SUM(SALARY)

---------------------------------

Ben 24000

Harsh46000

(2) SELECT NAME,salary from employee group by sno,salary;

OUTPUT :

NAME salary

------------------------

Harsh20000

Dhanraj 20000

Ashish 20000

Harsh26000

Ben 24000

(3) SELECT count(sno) from employee group by salary having


count(salary)>2;

OUTPUT : 3

(e) Use of join :

• Inner Join
SELECT employee.name,student.subject FROM employee INNER JOIN student
ON employee.sno = student.roll;

OUTPUT:

name subject

-----------------------

HarshEnglish

Dhanraj English

Ashish Maths

HarshHindi

Ben Hindi

• Left Join

SELECT employee.name,student.name FROM employee left JOIN student ON


employee.sno = student.roll;

name name

--------------------------

HarshSaraswat

Dhanraj Sachin

Ashish Durga

HarshShyam

Ben jimmy

• Right Join

SELECT employee.name,student.name FROM employee right JOIN student ON


employee.sno = student.roll;
name name

--------------------------

HarshSaraswat

Dhanraj Sachin

Ashish Durga

HarshShyam

Ben jimmy

You might also like