Monday, June 30, 2014

Simple Queries

Let's start with simple Queries with just one table employee(empID,empName,empSal)

1. SQL Statement to display all the records from the table employee:
 
select *
from employee;

2.SQL Statement  to display ID's and Name's of the employee's:

select empID,empName
from employee;

3.SQL Statement to display only the details of  employee with the ID '10101' :

select empID
from employee
where empID = '10101';

4.SQL Statement to display all the employee's whose name starts with S:

select *
from employee
where empName  like 'S%';


5.SQL Statement to display all the employee's with salary greater than 5000:
select *
from employee
where empSal >  5000;

6.SQL statement to display  all the employee's with salary greater than 5000 and less than 10,000:
select *
from employee
where empSal between '5000' and '10000';

7.SQL statement to display all the employee's details except Luke and John

select *
from employee
where empName <> 'Luke'  and empName <> 'John';

In Some Databases  we need to use != for not equal to

select *
from employee
where empName != 'Luke'  and empName != 'John';

Note:And Operator  displays the record only if both conditions are true

8.SQL statement to display only employee's Luke, John,Amy's details

select *
from employee
where empName IN ('Luke','John','Amy')