For T-SQL Beginners…

–Please do this of your own before you look into the answer…

/*
Objective – Need to find out the name of the passenger in each profession whose RlTktNo is highest
*/

INTERVIEW PREPARATION IN 30 MINs – Read Following wires for a quick interview preparation.

  1. SQL Server DBA “Interview Questions And Answers”
  2. SQL Azure Interview Questions and Answers Part – 1
  3. SQL Azure Interview Questions and Answers Part – 2
  4. Powershell Interview Questions and Answers

Data preparation

CREATE TABLE RailwayTicket(RlTktNo INT, PassengerName VARCHAR(40), Profession VARCHAR(40), Date datetime)
INSERT INTO RailwayTicket
SELECT 12020,’Passenger1′,’Teacher’,getdate()
UNION
SELECT 12021,’Passenger2′,’Teacher’,getdate()
UNION
SELECT 12022,’Passenger3′,’Teacher’,getdate()
UNION
SELECT 12023,’Passenger4′,’Businessman’,getdate()
UNION
SELECT 12024,’Passenger5′,’Businessman’,getdate()
UNION
SELECT 12025,’Passenger6′,’Businessman’,getdate()
UNION
SELECT 12026,’Passenger7′,’Scientist’,getdate()
UNION
SELECT 12027,’Passenger8′,’Scientist’,getdate()
UNION
SELECT 12028,’Passenger9′,’Scientist’,getdate()
UNION
SELECT 12029,’Passenger10′,’Scientist’,getdate()
UNION
SELECT 12030,’Passenger11′,’Scientist’,getdate()

Following is the query:

SELECT * FROM RailwayTicket
WHERE RlTktNo IN(
SELECT MAX(RlTktNo)
FROM RailwayTicket
GROUP BY Profession)

Regards,
Online MSSQL Classes @ http://tuitionaffordable.webstarts.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s