0% found this document useful (0 votes)
16 views5 pages

Exp 5

2erthj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Exp 5

2erthj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Student Name: Khushbu

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

SUBMITTED BY: SUBMITTED T O:


KHUSHBU Ms. SWETA
EXPERIMENT NO. - 05
Aim: Query the two cities in STATION with the shortest and longest CITY names, as well as their
respective lengths (i.e.: number of characters in the name). If there is more than one smallest or
largest city, choose the one that comes first when ordered alphabetically.
The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

Task to be done:

Query the shortest city name:

• Use the LENGTH() function to determine the city name length.


• Order the results alphabetically to pick the first one.

Query the longest city name:

• Similarly, use LENGTH() to find the city with the longest name.
• Order the results alphabetically to select the first one.

Select CITY and LENGTH:

• For both shortest and longest city names, retrieve the CITY and its respective LENGTH().

Limit the results:

• 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:

Step 1: Create the STATION Table

First, we need to create a table called STATION which includes relevant columns: CITY, LAT_N, and
LONG_W.

The structure of the table will be:

• CITY: This column stores the name of the city.


• LAT_N: Stores the northern latitude of the city (a floating-point number).
• LONG_W: Stores the western longitude of the city (a floating-point number).

Here's the SQL code to create this table:

CREATE TABLE STATION (


ID INT, -- Unique identifier for the record
CITY VARCHAR(21) , -- City name (up to 21 characters)
STATE VARCHAR(2), -- State code (2 characters)
LAT_N FLOAT, -- Northern latitude
LONG_W FLOAT -- Western longitude
);

Step 2: Insert Sample Data into the STATION Table

To test our query, let's insert some sample data:

INSERT INTO STATION (ID, CITY,STATE, LAT_N, LONG_W)


VALUES
(1, 'DEF',’TX’, 40.7128, -74.0060),
(2, 'ABC', ‘CA’,34.0522, -118.2437),
(3, 'PQRS', ‘PQRS’,51.5074, -0.1278),
(4, 'WXY', ‘WXY’,48.8566, 2.3522);

Step 3: Query to Find the City with the Shortest and Longest Names

Now we will write a query that finds:

• The city with the shortest name.


• The city with the longest name.
• Their respective lengths (number of characters).

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:

#Query for the shortest city name

(SELECT CITY, LENGTH(CITY) AS CITY_LENGTH


FROM STATION
ORDER BY LENGTH(CITY), CITY
LIMIT 1)

UNION ALL

#Query for the longest city name

(SELECT CITY, LENGTH(CITY) AS CITY_LENGTH


FROM STATION
ORDER BY LENGTH(CITY) DESC, CITY
LIMIT 1);

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.

You might also like