You are on page 1of 5

SQL COMMANDS

CREATE: This command is used to create a new table. With Create command a user can
define the structure of the new table in SQL i.e. with this command; we can define field
name, type & width for the table.
Ex: WAC to create a Table Emp having following fields Empno, Ename, Job, Sal, HireDate,
DeptNo.
Sol:
CREATE TABLE Emp
( Empno Int(4),
Ename VarChar(20),
Job VarChar(10),
Sal Decimal(7, 2),
HireDate Date,
DeptNo Int(2) );
INSERT: This command is used to insert a new record/row in any specific table.
Ex: WAC to insert a new record in Emp table & fill it with following values: 101,
‘SACHIN’, ‘Manager’, 15000, ‘2001-12-31’, 10
Sol:
Insert Into Emp Values
(101,‘SACHIN’,’Manager’,15000, ‘2001-12-31’, 10);

The INSERT statement adds a new row to emp table giving a value for every column in the
row. The data values are in the same order as the column names in the table.
Note: Data can be added only to some columns in a row by specifying the columns and their
data. For example if we want to insert only empno, ename and sal columns then we issue the
command as:
Insert into Emp (empno, ename, sal) values
(101, ‘SACHIN’, 15000);
The columns that are not listed in the Insert command will have their default value NULL.
Making Simple Queries: To fully use the power of an RDBMS, we need to communicate with
it. A powerful way of communicating with it is making queries. That is, asking the RDBMS
to show our desired data in desired format. This thing can be achieved through SELECT
command.
Select: This command is used to display all or some specific information from any specific
Table.
Ex: WAC to display Empno, Ename & salary for all the employees.
Sol: Select Empno, Ename, Sal from Emp;
Note: If we want to see the entire table i.e., every column of a table, we need not give a
complete list of columns. The asterisk (*) can be substituted for a complete list of columns.
Ex: WAC to display detail for all the Employess.
Sol: Select * from Emp;
Table: Emp
Empno Ename Job Sal HireDate DeptNo
101 SACHIN Manager 15000 2001-12-31 10
102 RAHUL Analyst 8000 2005-02-01 20
101 KAPIL Analyst 7000 2005-02-01 20
104 RAVI Salesman 5000 2008-12-31 30
105 SANJAY Manager 18000 2001-01-01 20
106 SOURAV Salesman 5000 2008-12-31 30

Using clauses (Options) for making queries:-


Distinct option: Using distinct option, we can eliminate (remove) duplicate values from the query
result.
Ex: WAC to display unique (distinct) job from the emp table.
Sol: Select Distinct Job from Emp;

All clause: If we want to display all the values of the table including duplicate rows then we
use ALL clause.
Ex: WAC to display all the Jobs from emp table.
Sol: Select ALL Job from Emp;

Note: The ALL keyword is optional i.e. we do not specify the ALL then also query will
display all the records. Ex: Select job from Emp;

As option: Using this option, we can change the name of any existing column or give any
specific name to the calculated fields in the query result.
Ex: WAC to display Ename as EmpName & HireDate as Date of Joining for all the
employees.
Sol: Select Ename AS EmpName, HireDate AS
‘Date of Joining’ from Emp;
Ex: WAC to display Empno, Ename, sal & Bonus for all the employees where Bonus is 20%
of sal.
Sol: Select Empno, Ename, Sal, Sal *.20 As Bonus from Emp;

Where option: This option is used to select any specific records for the output. A condition
is specified with the where option. All the records that satisfy the condition are displayed.
Ex: WAC to display detail for all the records having job manager.
Sol: Select * from Emp
Where Job = ‘Manager’;
Ex: WAC to display Ename & Job for all the emp having salary greater than 10000.
Sol: Select Ename, Job
from Emp where Sal >10000;
Ex: WAC to display detail for all the employees having job Salesman or Analyst.
Sol: Select * from Emp
where Job = ‘Salesman’ OR Job = ’Analyst’;
Ex: WAC to display names for all the emp having salary >15000 and Job is Analyst.
Sol: Select Ename from Emp
where Sal >15000 And Job = ’Analyst’;
Ex: WAC to display detail for all the employees having Deptno not equal to 20.
Sol: Select * from Emp
where DeptNo <> 20;
OR
Select * from Emp
where Not Deptno = 20;
BETWEEN - AND OPTION: This option is used to select the record between any specific
range. The lower value & upper value of range is also included.
Ex: WAC to display Empno, Ename for all the emp. having salary is in b/w 10000 to 20000.
Sol: Select Empno, Ename from Emp
where Sal Between 10000 And 20000;
OR
Select Empno, Ename from Emp
where Sal >=10000 And Sal <= 20000;
Ex: WAC to display list of all the emp. having salary is not in between 5000 to 8000.
Sol: Select * from Emp
where Not Sal Between 5000 And 8000;

Ex: WAC to display empno, Ename and HireDate for all the emp having hiredate in between
March 29, 2007 and July 07, 2007
Sol: Select Empno, Ename, HireDate from Emp
where HireDate Between ‘2007-03-29’
And ‘2007-07-07’;

IN & NOT IN operator: This option is used to select records from multiple set/list of
values. Some groups of values are specified with IN clause & the query will display rows
belonging to any specific value.
Ex: WAC to display detail for all the emp. having empno either 101, 103 or 105.
Sol: Select * from Emp
where Empno IN (101, 103, 105);
OR
Select * from Emp Where Empno = 101 OR
Empno = 103 OR Empno = 105;
Ex: WAC to display Ename & SAL for all the emp having dept not equal to sales or
purchase.
Sol: Select Ename, Salary from Employee
where Job Not IN (‘Salesman’, ‘Analyst’);
LIKE Operator: It is special operator that displays any particular information by giving
initial characters. Two special like operators are:
% : Stands for zero, one or more characters.
_ : Stands for zero or one character.

Ex: WAC to display detail for all the employees whose name starts with A.
Sol: Select * from Emp
where Ename Like ‘A%’;

Ex: WAC to display detail for all the emp having name starts with R (max. 4 CHARS).
Sol: Select * from Emp
where Ename Like ‘R_ _ _’;
Ex: WAC to display Empno, Ename & salary for all the employees having name contains A.
Sol: Select Empno, Ename, Sal from
Emp where Ename like ‘%A%’;

ORDER BY CLAUSE: This option is used to arrange output (result) of any specific table in
ascending or descending order. To specify the sort order, we may specify DESC for
descending order or ASC for ascending order. The default order is ascending.
Ex: WAC to display detail for all the employees in Ascending order of Ename.
Sol: Select * from Emp
Order by Ename;
Ex: WAC to display Ename & salary for all the employees having job Salesman in desc.
order of sal.
Sol: Select Ename, Sal from Emp
Where Job = ‘Salesman’
Order By Sal Desc;

You might also like