Exp 5
Exp 5
UID: 24MCI10177
Branch: MCA(AI&ML)
Section/Group: 3B
Semester: 1st
Date of Performance: 27 Sep. 2024
Subject Name: PL/SQL lab
Subject Code: 24CAP-602
where LAT_N is the northern latitude and LONG_W is the western longitude.
Task to be done:
• Similarly, use LENGTH() to find the city with the longest name.
• Order the results alphabetically to select the first one.
• For both shortest and longest city names, retrieve the CITY and its respective LENGTH().
• Use LIMIT 1 or an equivalent method (e.g., ROWNUM = 1) to ensure only one city is selected for
both the shortest and longest city names.
Combine results:
• Use a UNION query to combine the results for both shortest and longest city names into a single result
set.
Code for experiment/practical:
First, we need to create a table called STATION which includes relevant columns: CITY, LAT_N, and
LONG_W.
Step 3: Query to Find the City with the Shortest and Longest Names
To achieve this, we can use the LENGTH() function to find the length of each city name. We also need to
order the cities alphabetically in case there is a tie in city name length. We will use a UNION of two queries:
one for the shortest city name and another for the longest.
Here’s the SQL query:
UNION ALL
Output:
Learning outcomes:
Understand the use of the LENGTH() function: Learn how to compute the number of characters in a
string field (CITY).
Apply sorting with ORDER BY: Gain skills in sorting query results alphabetically when there are ties
(for shortest and longest city names).
Retrieve top records using LIMIT or ROWNUM: Learn how to limit query results to return only one
record for both shortest and longest city names.
Combine results using UNION: Understand how to merge two separate queries (for shortest and longest
cities) into a single result set.
Practice selecting specific fields: Focus on selecting the CITY and its respective length using the
SELECT clause, filtering for specific conditions.