Thursday, May 27, 2010

How to retrieve data from sql database( basic query)

suppose we have a database PAYROLL we have a table employee in database Payroll. following are the fields on which we will see all possible retrievals;

Empid Fname Lname Department Salary Unit

1 | ABD | KJH | dep1 |10000 | unit1

2 | DEF | DYTHG | dep2 |20000 | unit1

3 | GHI | GYUNBG | dep3 |40000 | unit2

4 | JKLM | IHIHJJU | dep4 |20000 | unit1

5 | NOPQW | GBTUNV | dep5 |10000 | unit3

6 | STUV | JHIUUV | dep6 |50000 | unit1

Command 1 : Select all entries from table

select * from payroll.employee

this command will give you whole table.

command 2: selecting only First name and Last name

select fname, lname from payroll.employee

defining user-defined column name: this can be done by following commands.

  • select ‘Department Name ‘= Department , ‘First Name’=fname, ‘Last Name’=Lname from payroll.employee
  • select Department ‘Department Name ‘ , fname ‘First Name’, Lname ‘Last Name’ from payroll.employee
  • select Department AS ‘Department Name ‘ , fname AS ‘First Name’, Lname AS ‘Last Name’ from payroll.employee

Now inserting the literals between the columns :

select empid, ‘ is the id of’, fname from payrol.employee

this command will give the following outtput:

Empid no column name Fname

1 | is the id of | ABD
2 | is the id of | DEF

3 | is the id of | GHI

4 | is the id of | JKLM

5 | is the id of | NOPQW

6 | is the id of| STUV

No comments:

Post a Comment