SQL server Interview queries


Here are some SQL queries asked frequently in interviews,
 

Write an SQL query to display the records whose date is having time stamp of "14" hrs.

CREATE TABLE VENKAT_TABLE(ID INT, MOBILE_NUMBER VARCHAR(15),AMOUNT INT, TIME_STAMP DATETIME)

INSERT INTO VENKAT_TABLE VALUES(1,'9840578690',10000,'1/1/2010 15:20:20')
INSERT INTO VENKAT_TABLE VALUES(1,'9840578690',5000,'1/1/2010 14:00:00')
 

-- The below command will fetch all the entries happened around 14 hours.
SELECT * FROM VENKAT_TABLE WHERE DATEPART(HH,TIME_STAMP) =14

-- The below command will fetch all the entries happened exactly at 14 hours.
SELECT * FROM VENKAT_TABLE WHERE DATEPART(HH,TIME_STAMP) =14 and DATEPART(mi,TIME_STAMP) =00
and DATEPART(s,TIME_STAMP) =00

 
Write an SQL query to display the unique number series (Starting 4 digits is the series) present in the table.

-- DISTINCT WILL PROVIDE YOU THE UNIQUE DATA
SELECT DISTINCT ID FROM VENKAT_TABLE
 

Write an SQL query to postfix '0' to the mobileno whose balance is more than 6000.

UPDATE VENKAT_TABLE SET MOBILE_NUMBER = MOBILE_NUMBER +'0' WHERE AMOUNT>6000
 
Cheers,