Sports - Part 1

       

Write an SQL query to fetch all the student names alphabetical order, followed by the first letter of each sports that the person has played enclosed in parentheses from the table.



Input :-

Sports table :-
ID is the Primary Key column for the Persons table.

Name

Sports

Roopa

Throwball

Deepa

Basketball

Harish

Football

Aarav

Cricket

Aarya

Volleyball

Anshuka

Hockey



Output :-

Name

Aarav(C)

Aarya(V)

Anshuka(H)

Deepa(B)

Harish(F)

Roopa(T)



Query :-
select concat(name,'(',left(Sports,1),')') as name
from Sports
order by name;

Explanation : - 

This SQL query retrieves the names from  the "Sports" table.


CONCAT(name, '(', LEFT(Sports, 1), ')') :- This is the expression used to concatenate the values of the 'name' column with a left parenthesis '(' and the first character of the 'Sports' column, followed by a right parenthesis ')'. The CONCAT function is used to combine multiple values into a single string.


ORDER BY clause is used to sort the results based on the 'name' column in ascending order.


In summary, the query retrieves the 'name' and 'Sports' columns from the "Sports" table, concatenates the 'name' value with the first character of the 'Sports' value, and sorts the result based on the 'name' column. The final result would be a list of concatenated strings showing the names of sports along with their first characters in parentheses, ordered alphabetically by name.






No comments

darkmode