Page 165 - Informatics_Practices_Fliipbook_Class12
P. 165
Query: To display the Department numbers of all employees.
SELECT Dept_No
FROM EMPLOYEE;
On executing the above statement, SQL will produce output shown in Table 4.11.
Dept_No
2
1
2
4
1
4
5
2
4
5
Table 4.11: Department numbers of all employees
4.4.10 DISTINCT clause
We see in Table 4.11 that the execution of the above queries yields the department numbers, with multiple values.
Table 4.11 will look more elegant if we remove the repeating tuples. For this purpose, the keyword DISTINCT is used
in the SELECT clause to retrieve unique values of an attribute. The resulting SQL statement would be:
SELECT DISTINCT Dept_No
FROM EMPLOYEE;
Execution of the above SELECT statement will produce the output shown in Table 4.12:
Dept_No
2
1
1
5
Table 4.12: Unique department numbers from EMPLOYEE table
4.4.11 ORDER BY CLAUSE
The SQL ORDER BY clause sorts data on one or more columns in ascending or descending order. By default, SQL sorts
the data in ascending order of the specified attribute.
ORDER BY clause: sorts data based on specified attribute(s).
Default order: Ascending, or specify DESC.
To display ID and the first name of all employees arranged alphabetically according to their first names, we use the
statement:
SELECT ID, FName
FROM EMPLOYEE
ORDER BY FName;
Database Query using SQL 151

