Interview Answer

Any code of your own that you havenít looked at for six or more months might as well have been written by someone else. Eaglesonís Law

How to select first 5 records from a table?

January 1, 1970 12:00 am

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.