Interview Answer

Increasingly, people seem to misinterpret complexity as sophistication, which is baffling - the incomprehensible should cause suspicion rather than admiration. Niklaus Wirth

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.