EXP No.
AIM: Perform Simple queries, string manipulation operations and aggregate functions.
1. SIMPLE SQL QUERIES
These are basic queries to retrieve data from a table.
Example Table: students
i name age marks city
d
1 Alice 20 85 New York
2 Bob 22 78 London
3 Charlie 21 92 Paris
4 David 23 60 New York
SIMPLE QUERIES EXAMPLES
1. Retrieve all data from the table:
SELECT * FROM students;
2. Select specific columns:
SELECT name, age FROM students;
3. Filter records using WHERE clause:
SELECT * FROM students WHERE city = 'New York';
4. Sort the records by marks (descending):
SELECT * FROM students ORDER BY marks DESC;
5. Find students above 21 years of age:
SELECT * FROM students WHERE age > 21;
2. STRING MANIPULATION OPERATIONS
These are operations performed on text (string) data like names, cities, etc.
1. Convert name to uppercase:
SELECT UPPER(name) AS Uppercase_Name FROM students;
Output:
Uppercase_Name
ALICE
BOB
CHARLIE
DAVID
2. Get the first 3 letters of each name:
SELECT LEFT(name, 3) AS Short_Name FROM students;
Output:
Short_Name
Ali
Bob
Cha
Dav
3. Concatenate name and city:
SELECT CONCAT(name, ' from ', city) AS Description FROM students;
Output:
Description
Alice from New York
Bob from London
Charlie from Paris
David from New York
4. Replace city name:
SELECT REPLACE(city, 'New York', 'Los Angeles') AS New_City FROM students;
3. AGGREGATE FUNCTIONS
Aggregate functions summarize the data, such as getting totals, averages, counts,
etc.
1. Count total students:
SELECT COUNT(*) AS Total_Students FROM students;
Output:
Total_Students
4
2. Calculate average marks:
SELECT AVG(marks) AS Average_Marks FROM students;
Output:
Average_Marks
78.75
3. Find the highest marks:
SELECT MAX(marks) AS Highest_Marks FROM students;
Output:
Highest_Marks
92
4. Find the lowest marks:
SELECT MIN(marks) AS Lowest_Marks FROM students;
Output:
Lowest_Mark
s
60
5. Sum of all marks:
SELECT SUM(marks) AS Total_Marks FROM students;
Output:
Total_Marks
315
6. Group students by city and count them:
SELECT city, COUNT(*) AS Student_Count FROM students GROUP BY city;
Output:
city Student_Count
New York 2
London 1
Paris 1
Combined Query (String + Aggregate)
Suppose you want to get the highest mark holder's name in uppercase:
SELECT UPPER(name) AS Topper, MAX(marks) AS Highest_Marks
FROM students;
Output:
Topper Highest_Marks
CHARLIE 92
Conclusion: Hence we have performed Simple queries, string manipulation
operations and aggregate functions.