OPERATORS IN SQL


COMPARISON OPERATORS:
              SQL provides the following comparison operators
 
Symbol
Meaning
=
Equal to
< 
Less than
<=
Less than or equal to
> 
Greater than
>=
Greater than or equal to
< > or   ! =
Not equal to

ARITHMETIC OPERATORS:
              SQL provides the following arithmetic operators. We can use arithmetic operators with table attributes in a column list or in  a conditional expression.

Arithmetic operator
Description
+
Add
-
Subtract
*
Multiply
/
Divide
^
Raise to the power of (some application use ** instead of ^)

LOGICAL OPERATORS:
SQL allows having multiple conditions in a query through the use of logical operators. The logical operators are: AND, OR, NOT .The logical operators are used to connect the Boolean expressions in the where clause.

SPECIAL OPERATORS:
ANSI –standard   SQL allows the use of special operators in conjunction with the WHERE clause. These special operators include:
BETWEEN: used to check whether an attribute value is within a range.
IS NULL: used to check whether an attribute value is null.
IN: used to check whether an attribute value matches any value within a value list.
EXISTS: used to check whether a sub query returns any rows.
LIKE: used to check whether an attribute value matches a given string pattern.
                          The LIKE special operator is used in conjunction with wildcards to find patterns within string attributes. Standard SQL allows to use the percent sign (%) and underscore ( _ ) wildcard characters to make matches when the entire string is not known:
Ø  % Means any and all following or preceding characters are eligible. For example ‘J%’ includes Johnson, James, and July. ‘Jo %’ includes Johnson, Jones. ‘%n ‘includes Johnson, Jagan, Kiran.
Ø  _ means any one character may be substituted for the underscore
 AGGREGATE FUNCTIONS:
              SQL can perform various mathematical summaries, such as counting the number of rows that contain a specified condition, finding the minimum or maximum values for some specified attribute, summing and averaging the values in a specified column.
  Some basic SQL aggregate functions:

Function           Output
COUNT            The number of rows containing non-null values.
MIN                  The minimum attribute value encountered in a given column
MAX                The maximum attribute value encountered in a given column
SUM                 The sum of all values for a given column
AVG                 The arithmetic mean(average) for a specified column


GROUPING DATA:
Frequency distribution can be created quickly and easily using the GROUP BY clause within the SELECT statement. The syntax is:
             
              SELECT         column list
              FROM             table list
              [WHERE        condition list]
              [GROUP BY column list]
              [HAVING      condition list]
              [ORDER BY column list [ASC|DESC]];

  The GROUP BY clause is generally used when you have attributes columns combined with aggregate functions in the SELECT statement.
  The GROUP BY clause is valid only when used in conjunction with one of the SQL aggregate functions such as COUNT, MIN, MAX, AVG and SUM.

EXAMPLE-SQL OPERATORS

CONSIDER THE FOLLOWING EMPLOYEE TABLE AND WRITE THE QUERIES AND CORRESPONDING RESULT FOR EACH QUERY.

EMPLOYEE
EMP_NUM
EMP_NAME
JOB_TYPE
HIREDATE
EMP_SAL
EMP_LOC
 1001
 KRISHNA
MANAGER 
 01-JAN-2011
 50000
 NANDYAL
 1002
 CLERK
 01-JAN-2011
 15000
 KURNOOL
 1003
 RAM
 CLERK
 16-AUG-2011
 15000
NANDYAL
 1004
 MAHESH
 ASSTMANAGER
 03-MAY-2011
 35000
 HYDERABAD
 1005
 VIGNESH
 ACCOUNTANT
 01-JAN-2011
 20000
 KADAPA
 1006
 NAGENDRA
 MECHANIC
 01-JAN-2011
 10000
 KADAPA
 1007
 KIRAN
 CLERK
 08-JUN-2011
 15000
HYDERABAD
 1008
 LOKESH
 ATTENDER
 01-JAN-2011
 12000
KURNOOL
 1009
 MOHAN
 ATTENDER
 16-AUG-2011
 12000
 KADAPA
 1010
 PRAVEEN
ADMINOFFICER
 02-JAN-2011
 40000
 NANDYAL




1.      Find the employee whose salary is greater than 25000
SQL> select *
  2   from employee
  3   where emp_sal> 25000;

   EMP_NUM EMP_NAME   JOB_TYPE     HIREDATE     EMP_SAL              EMP_LOC
---------------   ------------------- ------------ --------- ---------- ---------------------------------------
            1001    krishna             manager             01-JAN-11                50000              nandyal
            1004    mahesh            asstmanager       03-MAY-11              35000              hyderabad
            1010    praveen            adminofficer     02-JAN-11                40000             nandyal

2.      Find the name of employees whose salary is 50000
SQL> select emp_name
  2   from employee
  3   where emp_sal=50000;

EMP_NAME
---------------
krishna
3.      Find the employee who is manager and belongs to nandyal
SQL>select *
             2       from employee
             3       where job_type='manager' and emp_loc='nandyal';

              EMP_NUM  EMP_NAME   JOB_TYPE     HIREDATE      EMP_SAL             EMP_LOC
----------------  ------------------  ----------------   ------------------  -------------   --------------
              1001              krishna             manager             01-JAN-11               50000  nandyal

4.      Find the employee who are either manager or belongs to nandyal
SQL> select *
  2       from employee
  3       where job_type='manager' or emp_loc='nandyal';
   EMP_NUM             EMP_NAME   JOB_TYPE     HIREDATE     EMP_SAL EMP_LOC
---------------- ------------------ ------------------ -----------------  ------------ ----------------
             1001   krishna             manager            01-JAN-11              50000     nandyal
            1003    ram                  clerk                16-AUG-11              15000     nandyal
            1010    praveen            adminofficer   02-JAN-11              40000     nandyal

5.      Find employee whose salary is not less than 20000
SQL> select *
   2      from employee
1         where not(emp_sal<20000);

(or)

  SQL> select *
    2      from employee
    3      where emp_sal>=20000;

   EMP_NUM EMP_NAME   JOB_TYPE     HIREDATE    EMP_SAL EMP_LOC
---------------- ------------------ -----------------  ----------------- -------------  ------------
            1001    krishna              manager          01-JAN-11                     50000       nandyal
            1004    mahesh            asstmanager     03-MAY-11       35000       hyderabad
            1005    vignesh            accountant       01-JAN-11         20000       kadapa
            1010    praveen            adminofficer   02-JAN-11         40000       nandyal

6.      Find employee whose salary is in between 20000 and 40000
SQL> select *
  2      from employee
  3      where emp_sal between 20000 and 40000;

   EMP_NUM EMP_NAME     JOB_TYPE     HIREDATE     EMP_SAL EMP_LOC
--------------- ---------------------  ----------------- ----------------- -------------- ---------------
            1004    mahesh                asstmanager     03-MAY-11    35000          hyderabad
            1005    vignesh                accountant       01-JAN-11      20000          kadapa
            1010    praveen                adminofficer    02-JAN-11      40000         nandyal

7.      Find the name and location of employee who location is nandyal or Kurnool
SQL> select *
  2      from employee
  3      where emp_loc in('nandyal','kurnool');
                         

(or)

SQL> select *
  2      from employee
  3      where emp_loc='nandyal' or emp_loc='kurnool';

   EMP_NUM EMP_NAME   JOB_TYPE     HIREDATE    EMP_SAL EMP_LOC
--------------- -------------------  ----------------- ----------------  ------------- ------------
            1001    krishna             manager           01-JAN-11           50000    nandyal
            1002                            clerk                01-JAN-11          15000     kurnool
            1003    ram                  clerk                16-AUG-11        15000     nandyal
            1008    lokesh              attender           01-JAN-11         12000     kurnool
            1010    praveen            adminofficer   02-JAN-11         40000     nandyal


8.      Find the rows from employee table whose EMP_NAME column values are null
SQL> select *
  2      from employee
  3      where emp_name is null;

     EMP_NUM    EMP_NAME     JOB_TYPE   HIREDATE EMP_SAL   EMP_LOC
------------------   ------------------- ---------------- --------------- --------------  ---------------
                 1002                               clerk             01-JAN-11           15000    kurnool


9.      Find the name of employees whose name starts with ‘k’
SQL> select emp_name
  2      from employee
  3      where emp_name like 'k%';

EMP_NAME
---------------
krishna
kiran

10.  Find the name of employees whose name ends with ‘esh’
SQL> select emp_name
  2      from employee
  3      where emp_name like '%esh';

EMP_NAME
---------------
mahesh
vignesh
lokesh

11.  Find the name of employees in which the second character is ‘a’
SQL> select emp_name
  2       from employee
  3       where emp_name like '_a%';

EMP_NAME
---------------
ram
mahesh
nagendra

12.  Find number of employees.
SQL> select count(*)
  2       from employee;

  COUNT(*)
---------------
        10

13.  Find minimum salary of the employee

SQL> select min(emp_sal)
  2      from employee;

MIN(EMP_SAL)
------------
       10000

14.  Find maximum salary of the employee
SQL> select max(emp_sal)
  2       from employee;

MAX(EMP_SAL)
----------------------
       50000

15.  Find total salary of all employees
SQL> select sum(emp_sal)
  2       from employee;

SUM(EMP_SAL)
----------------------
      224000
16.  Find the average salary of the employees
SQL> select avg(emp_sal)
  2       from employee;

AVG(EMP_SAL)
---------------------
       22400

17.  Find number of different jobs in the company
SQL> select count(distinct job_type)
  2       from employee;

COUNT(DISTINCT JOB_TYPE)
-------------------------------------
                      7

18.  Find employees whose salary is less than average salary
SQL> select *
  2     from employee
2        where emp_sal<(select avg(emp_sal)
from employee);

  EMP_NUM EMP_NAME    JOB_TYPE    HIREDATE     EMP_SAL   EMP_LOC
--------------- --------------------  ----------------  ------------------ ------------- ------------------
            1002                             clerk                01-JAN-11               15000    kurnool
            1003    ram                   clerk                16-AUG-11              15000    nandyal
            1005    vignesh             accountant       01-JAN-11               20000    kadapa
            1006    nagendra          mechanic         01-JAN-11               10000    kadapa
            1007    kiran                clerk                 08-JUN-11               15000    hyderabad
            1008    lokesh              attender            01-JAN-11               12000    kurnool
            1009    mohan              attender           16-AUG-11             12000     kadapa

7 rows selected.

19.  What is the output of the following query:
      SQL>       SELECT EMP_NAME, EMP_LOC
                       FROM EMPLOYEE
GROUP BY EMP_LOC;
            select emp_name, emp_loc
           *
             ERROR at line 1:
ORA-00979: not a GROUP BY expression

20.  Find the number of employees for each job
SQL> select job_type,count(emp_name)
  2       from employee
  3       group by job_type;

JOB_TYPE     COUNT(EMP_NAME)
----------------- ---------------------------
attender                                                2
asstmanager                                          1
clerk                                                     2
accountant                                            1
adminofficer                                                    1
manager                                                1
mechanic                                              1

7 rows selected.

21.  What is the maximum salary for each job
SQL> select job_type, max(emp_sal)
  2       from employee
  3        group by job_type;

JOB_TYPE     MAX(EMP_SAL)
-----------------   ----------------------
attender                                    12000
asstmanager                  35000
clerk                            15000
accountant                   20000
adminofficer               40000
manager                       50000
mechanic                     10000

7 rows selected.

22.  Find the number of each job and name the column that gives number of jobs as jobnum

SQL> select job_type, count(job_type) as jobnum
  2       from employee
  3       group by job_type;

JOB_TYPE                 JOBNUM
-------------------- ------------------
attender                                   2
asstmanager                             1
clerk                                        3
accountant                               1
adminofficer                           1
manager                                   1
mechanic                                 1

7 rows selected.
23.  Find the number of employees of each location
SQL> select emp_loc, count(emp_num)
  2       from employee
  3      group by emp_loc;

EMP_LOC      COUNT(EMP_NUM)
------------------ ------------------------
hyderabad                               2
kadapa                                     3
nandyal                                   3
kurnool                                    2
24.  Find the name and salary of the employee with maximum salary
SQL> select emp_name, emp_sal
  2      from employee
  3      where emp_sal=(select max(emp_sal) from employee);

EMP_NAME           EMP_SAL
---------------------- --------------
krishna                             50000
25.  Find the count of employees for each job so that at least two of the employees had salary greater than 10000

SQL> select job_type ,count(emp_num)
  2        from employee
  3        where emp_sal>10000
  4        group by job_type
  5        having count(emp_num)>=2

JOB_TYPE     COUNT(EMP_NUM)
----------------- -------------------------
attender                                           2
clerk                                                3