Oct 29, 2012

How to use GROUP BY and ORDER BY together in SQL


Step 1



First you need to create one table named employee with 9 fields.



CREATE TABLE employee (


 


    id int unsigned not null auto_increment primary key,


    firstname varchar(20),


    lastname varchar(20),


    title varchar(30),


    age int,


    yearofservice int,


    salary int,


    perks int,


    email varchar(60)


); 


 


Step 2


 


Insert some dummy values into the employee table


 



INSERT INTO employee (firstname, lastName, title, age, yearofservice, 


salary, perks, email) values ("James", "John", 


"Programmer", 31, 3, 120000, 25000, "jj@gmail.com");


 



INSERT INTO employee (firstname, lastName, title, age, yearofservice, 


salary, perks, email) values ("Joe", "Mathew", 


"Teacher", 42, 4, 186000, 32000, "jm@gmail.com");


 



INSERT INTO employee (firstname, lastName, title, age, yearofservice, 


salary, perks, email) values ("Jobin", "Thomas", 


"Programmer", 28, 1, 236000, 36800, "jt@gmail.com");






Step 3



Use the following code to display the details.



select title, count(*) 


from employee


GROUP BY title 


ORDER BY age;




 

No comments:

Post a Comment