Topic: SQL (Answer this as SQL query (preferably mySql).Only in SQL. DO NOT answer in PHP or other languages)
Employee details
There is a table of employee location information calledemp_details. Each record contains a person’s name and the citywhere they live.
Write a query to return a list of items,
Teams are formed within these rules:
Team members must live in the city they represent.
For each city, create teams of 3 until there are fewer than 3who are unassigned.
When there are fewer than 3 people unassigned in a city, theyform a team.(See New York in the sample data tables section.)
Report requirements:
There should be 3 columns: the city name, a comma-delimited listof upto 3 players, and the team’s name.
The cities should be ordered alphabetically.
Players are selected in the order they occur in the table.
Their names should be in order alphabetically within thecomma-delimited list.
Team names are ‘Team’ plus a number. For example, the firstrow’s team is Team1, then Team2, and so on through Team20.
Only show the first 20 rows.
Schema
emp_details
Name
Type
Description
EMP_NAME
VARCHAR
Employee Name
CITY
VARCHAR
Name of the City
Sample Data Tables
emp_details
EMP_NAME
CITY
Sam
New York
David
New York
Peter
New York
Chris
New York
John
New York
Steve
San Francisco
Rachel
San Francisco
Robert
Los Angeles
Output:
Los Angeles Robert Team 1
New York David, Peter, Sam, Team 2
NewYork Chris, John Team 3
San Francisco Rachel, Steve Team 4
Explanation:
It will group the employees based on cities and restrict thenumber of employees as per row to 3 at max, also team names are tobe given for each row.
New York Is an example of a city with 2 teams. Grab the first 3names, Sam, David, Peter for the first team, sort the names andreport: David, Peter, Sam. Noe grab the last 2, Chris, John, sortthem: Chris, John.
Topic: SQL (Answer this as SQL query (preferably mySql). Only in SQL. DO NOT answer in PHP or other languages) Employee
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am