0% found this document useful (0 votes)
38 views9 pages

SQL Tutorial (Chap8)

Uploaded by

jaggu011
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
38 views9 pages

SQL Tutorial (Chap8)

Uploaded by

jaggu011
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

 Chapter 8: DRL & Clauses

 SQL Tutorial – SELECT

o SELECT statement is used to retrieve data from a table in the database.


o Retrieval of data can be from specific columns or from all the columns in the table.
o To create a simple SQL SELECT Statement, you must specify the column(s) name and the table
name.

By using SELECT we can retrieve either few columns or all columns.

o Table-name is the name of the table from which the information is retrieved.
o Col1, col2, coln means one or more columns of table from which data is retrieved.

Selecting few Columns:-


SQL> SELECT empno, ename, job

FROM emp;
Output:-
‘SELECT’ clause in the query specifies columns that we wanted to select.
‘FROM’ clause in the query specifies data sources. If we have multiple data sources then we
should specify joins between them.
Selecting ALL COLUMNS:-

SQL> SELECT * FROM emp;


Output:-
SQL Tutorial - Where Clause
WHERE clause is used to specify the conditions while retrieving or manipulating data.

It is not necessary that we should always fetch all records from the table. Using where clause we
can restrict the data by using conditions.

Example:- If we wanted to display whose salary is equal to 2000, below is the query.
SQL> SELECT *FROM emp WHERE sal =2000;

SQL Tutorial - ORDER BY

 ‘ORDER BY’ clause is used to arrange the select statement output in ASCENDING or DESCENDING
order.
 ‘ORDER BY’ clause Supports with all types of data.
By default oracle will use ascending order. If we want to sort in descending order then specify “ desc “

Example: Display employee records order by ename.


SQL> SELECT *
FROM emp
ORDER BY ename;
Here we can see that ‘ENAME’ values resulted in ‘ASCENDING ORDER’.
Example: Display employee records order by ‘ename’ in descending order. Here we need to explicitly
specify the keyword ‘DESC’.
SQL> SELECT *
FROM EMP
ORDER BY ename DESC;

Ordering the result based on a numeric column.

SQL> SELECT *
FROM emp
ORDER BY sal;
We can as well order the result using a column sequence.

SQL> SELECT empno,ename,job,sal


FROM emp
ORDER BY 2;

Here 2 represents second column. So, data will be displayed with ename in ascending order.

SQL Tutorial - GROUP BY

 Group by clause is used to create groups of related information.


 Columns used in ‘select’ must be used with ‘group by’, otherwise system does not recognize it as a group by expression.
Example:-
In the below example we are trying to display sum of salaries at each dept level.

When SQL statement is run, data fetched from the database is grouped first based on deptno and then sum()

function is applied to get the desired result.


SQL> SELECT deptno, SUM(sal)
FROM emp
GROUP BY deptno;

In this example we are trying to group the records using two columns deptno and job
SQL> SELECT deptno,job, SUM(sal)
FROM emp
GROUP BY deptno,job;

Suppose if we try to apply summary function on a column along displaying other columns and not using group by clause
will result in error.
SQL> SELECT empno, ename, deptno, MAX (sal) FROM emp;

Output:

ERROR at line 1:
ORA-00937: NOT single-GROUP groups FUNCTION
Even if we specify only one column in group by then system will raise an error
SQL> SELECT empno, ename, deptno, MAX (sal) FROM emp GROUP BY deptno;

Output:

ERROR at line 1:
ORA-00979: NOT a GROUP BY expression
Rules:-
 The columns specified in the group by clause should be specified in select clause.
 If not in Group by then that column in select should be applies with summary functions.
 Summary columns and column aliases are not allowed in group by clause.

SQL Tutorial - Having Clause


 Having clause is used to restrict the records that are grouped using group by clause.
 Having clause is used to apply conditions on the summary results.
 Having clause is just like where clause but it can be used only with group by as we cannot use
where clause in group by.

In the below example we are trying to fetch all deptno and job records whose aggregated salary
of its

employees is greater than 3000 ( Sum(sal) > 3000 ).

Having clause gets executed after the records are fetched and grouped.
SQL>SELECT deptno,job, SUM(sal) tsal
FROM emp
GROUP BY deptno,job
HAVING SUM(sal) > 3000;
In this example, after applying the having clause we are trying to order the records by DEPTNO.
SQL> SELECT deptno,job,SUM(sal) tsal
FROM emp
GROUP BY deptno,job
HAVING SUM(sal) > 3000
ORDER BY deptno;

In the below example, ‘where condition’ is applied first and after that HAVING clause is applied.
SQL> SELECT deptno, COUNT (*)
FROM emp
WHERE job='CLERK'
GROUP BY deptno
HAVING COUNT (*)>=2;

ORDER OF SQL STATEMENT EXECUTION

 Group the rows together based on group by clause.


 Calculate the group functions for each group.
 Choose and eliminate the groups based on the having clause.
 Order the groups based on the specified column.

You might also like