0% found this document useful (0 votes)
3 views7 pages

Python + SQL

The document provides a comprehensive overview of various mathematical, text/string, date, and aggregate functions used in SQL, along with examples for each function. It also includes a section on Pandas DataFrame operations, detailing methods for creating, displaying, and manipulating data within DataFrames. Each function and operation is accompanied by example code and expected output for clarity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Python + SQL

The document provides a comprehensive overview of various mathematical, text/string, date, and aggregate functions used in SQL, along with examples for each function. It also includes a section on Pandas DataFrame operations, detailing methods for creating, displaying, and manipulating data within DataFrames. Each function and operation is accompanied by example code and expected output for clarity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🔢 1.

Math Functions
1. POWER(x, y)
Returns x raised to the power y.

SELECT POWER(2, 3) AS Result;

Output: 8

2. ROUND(number, decimal_places)
Rounds a number to given decimal places.

SELECT ROUND(12.5678, 2) AS RoundedValue;

Output: 12.57

3. MOD(x, y)
Returns remainder when x is divided by y.

SELECT MOD(17, 5) AS Remainder;

Output: 2

🔤 2. Text/String Functions
Assume table students(name).

1. UCASE() / UPPER()
Converts text to uppercase.

SELECT UPPER('sonbhadra') AS UpperName;

Output: SONBHADRA

2. LCASE() / LOWER()
Converts text to lowercase.

SELECT LOWER('MVM PUBLIC SCHOOL') AS LowerName;

Output: mvm public school

3. MID(text, start, length)


OR

SUBSTRING(text, start, length)


Returns part of a string.

SELECT MID('Informatics', 2, 5) AS Part1;


SELECT SUBSTRING('Informatics', 2, 5) AS Part2;

Output: nform

4. LENGTH(text)
Returns length of a string.

SELECT LENGTH('Krishna') AS Len;

Output: 7

5. LEFT(text, n)
Returns first n characters.

SELECT LEFT('Informatics', 5) AS FirstPart;

Output: Infor

6. RIGHT(text, n)
Returns last n characters.

SELECT RIGHT('Informatics', 4) AS LastPart;

Output: tics
7. INSTR(text, substring)
Returns position of substring.

SELECT INSTR('Informatics', 'mat') AS Position;

Output: 6

8. LTRIM(text)
Removes spaces from left side.

SELECT LTRIM(' India') AS TrimLeft;

Output: India

9. RTRIM(text)
Removes spaces from right side.

SELECT RTRIM('India ') AS TrimRight;

Output: India

10. TRIM(text)
Removes spaces from both sides.

SELECT TRIM(' Krishna Mishra ') AS TrimBoth;

Output: Krishna Mishra

📅 3. Date Functions

1. NOW()
Returns current date + time.

SELECT NOW() AS CurrentDateTime;

Output: 2025-12-13 10:25:00 (example)


2. DATE()
Extract only date from a datetime value.

SELECT DATE(NOW()) AS TodayDate;

3. MONTH(date)
Returns month number (1–12).

SELECT MONTH('2025-06-15') AS MonthNo;

Output: 6

4. MONTHNAME(date)
Returns full month name.

SELECT MONTHNAME('2025-06-15') AS MonthName;

Output: June

5. YEAR(date)
Return year.

SELECT YEAR('2025-06-15') AS YearValue;

Output: 2025

6. DAY(date)
Day of month (1–31).

SELECT DAY('2025-06-15') AS DayNumber;

Output: 15

7. DAYNAME(date)
Returns weekday name.
SELECT DAYNAME('2025-06-15') AS DayName;

Output: Sunday

📊 4. Aggregate Functions
Assume table Marks(student, score)

+----------+-------+
| student | score |
+----------+-------+
| Ravi | 78 |
| Anita | 92 |
| Mohan | 65 |
| Neha | 92 |
+----------+-------+

1. MAX()
SELECT MAX(score) AS HighestMarks FROM Marks;

Output: 92

2. MIN()
SELECT MIN(score) AS LowestMarks FROM Marks;

Output: 65

3. AVG()
SELECT AVG(score) AS AverageMarks FROM Marks;

Output: 81.75

4. SUM()
SELECT SUM(score) AS TotalMarks FROM Marks;

Output: 327

5. COUNT()
Counts number of rows.

SELECT COUNT(*) AS TotalStudents FROM Marks;

Output: 4

Counts non-NULL values:

SELECT COUNT(score) AS TotalScores FROM Marks;

Here is a clean, clear, class-friendly TABLE for Pandas DataFrame operations exactly matching your
syllabus.
(You can paste this directly into your notes or question papers.)

📘 PANDAS – DATA FRAME FUNCTIONS


(TABLE FORMAT)
Operation /
Topic Example Code Explanation
Function
Creates
From python import pandas as pd s1 = [Link]([1,2,3]) DataFrame
Creation of
Dictionary s2 = [Link](['A','B','C']) df = using multiple
DataFrame [Link]({'Roll': s1, 'Name': s2})
of Series Series as
columns
Each
dictionary
From List of python data = [{'Name':'A','Marks':90},
Creation becomes a row
Dictionaries {'Name':'B','Marks':85}] df = [Link](data)
in the
DataFrame
Loads data
From CSV from a CSV
Creation python df = pd.read_csv("[Link]")
File file into a
DataFrame
Displays all
Show full
Display python print(df) rows and
DataFrame
columns
Shows first 5
Display Head() python [Link]()
rows
Shows last 5
Display Tail() python [Link]()
rows
Access each
Iterate
Iteration python for i, r in [Link](): print(r["Name"]) row one by
through rows
one
Operations Adds new
Add column python df["Total"] = df["Marks"] + 5
on Columns column
Operations Select python df["Name"] Returns a
Operation /
Topic Example Code Explanation
Function
column
on Columns column
(Series)
Operations Delete python [Link]("Marks", axis=1)
Deletes a
on Columns column column
Operations Rename python [Link](columns={'Name':'Student'})
Renames a
on Columns column column
Operations Select row python [Link][1]
Selects row by
on Rows (loc) label
Operations Select row python [Link][0]
Selects row by
on Rows (iloc) position
Operations Deletes row
Delete row python [Link](2, axis=0)
on Rows with index 2
Operations Adds a new
Add row python [Link][4] = ['D', 88]
on Rows row
Label-based
Access by
Indexing indexing python [Link][0:2, ['Name','Marks']]
labels
(loc)
Position-
Access by
based
Indexing python [Link][0:3, 0:2] numeric
indexing
positions
(iloc)
Returns rows
Boolean where
Indexing python df[df["Marks"] > 80]
Indexing condition is
True

You might also like