Interview Answer

Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter. Eric S. Raymond

How to select first 5 records from a table?

April 10, 2016 8:07 pm

Comments



How to select first 5 records from a table?


The employee table contains the following fields

													
													Name           Null     Type         
													-------------- -------- ------------ 
													EMPLOYEE_ID    NOT NULL NUMBER(6)    
													FIRST_NAME              VARCHAR2(20) 
													LAST_NAME      NOT NULL VARCHAR2(25) 
													EMAIL          NOT NULL VARCHAR2(25) 
													PHONE_NUMBER            VARCHAR2(20) 
													HIRE_DATE      NOT NULL DATE         
													JOB_ID         NOT NULL VARCHAR2(10) 
													SALARY                  NUMBER(8,2)  
													COMMISSION_PCT          NUMBER(2,2)  
													MANAGER_ID              NUMBER(6)    
													DEPARTMENT_ID           NUMBER(4)   
													

Solution

   					 
													-- Oracle Soltuion
													SELECT * FROM HR.employees WHERE ROWNUM <= 5;

													-- Generic Solution
													SELECT  * FROM HR.employees emp
													WHERE (SELECT count(*) FROM HR.employees i 
														WHERE i.employee_id < emp.employee_id) < 5
																	    

Comments



Please login to add comments.