0% found this document useful (0 votes)
4 views4 pages

Python and SQL Code Snippets

The document contains various code snippets and SQL queries for different functionalities including checking for palindromes, generating wavy patterns, finding unique students, salary analysis, and retrieving the latest hire. It also includes SQL commands for counting orders by type in specific states, joining tables, and selecting products with their respective companies. Each code section is structured to be executed in a main function or directly through SQL commands.

Uploaded by

memef1052
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)
4 views4 pages

Python and SQL Code Snippets

The document contains various code snippets and SQL queries for different functionalities including checking for palindromes, generating wavy patterns, finding unique students, salary analysis, and retrieving the latest hire. It also includes SQL commands for counting orders by type in specific states, joining tables, and selecting products with their respective companies. Each code section is structured to be executed in a main function or directly through SQL commands.

Uploaded by

memef1052
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

PALINDROME

import sys

def isPalindrome(s): # Write your code here


if s == s[::-1]:
return 1
else:
return 0

def main():
s = [Link]().strip('\n')
result = isPalindrome(s)
print(result)

if __name__ == "__main__":
main()

WAVY PATTERN GENERATOR

def make_wave(arr):

[Link]()

for i in range(0, len(arr) - 1, 2):


arr[i], arr[i + 1] = arr[i + 1], arr[i]

return arr

if __name__ == "__main__":

input_list = list(map(int, input("Enter a list of integers separated by


spaces: ").split()))
output_list = make_wave(input_list)

print (‘Wavy pattern list:’, output_list)

UNIQUE STUDENTS

def unique(n, DS, m, ML):


return "\n".join(sorted(list(set(DS) - set(ML))))

def main():
n = int(input())
DS = [None] * n
for j in range(n):
DS[j] = input()

m = int(input())
ML = [None] * m
for j in range(m):
ML[j] = input()

result = unique(n, DS, m, ML)


print(result)

if __name__ == "__main__":
main()
GUJARAT (SQL)

SELECT COUNT(*) AS oc, Order_City


FROM orders
WHERE Order_State = ‘Gujarat’
AND Order_Status = ‘PENDING’
GROUP BY Order_City
ORDER BY oc, Order_City ASC;

STRING REVERSER

import sys

def strReverse(s):
return " ".join(reversed([Link](" ")))

def main():
s = [Link]().strip('\n')
result = strReverse(s)
print(result)

if __name__ == "__main__":
main()

SALARY ANALYSIS (SQL)

WITH CTE_EMP AS (
SELECT EMP_NO,
EMP_NAME,
EMP_SALARY,
AVG(EMP_SALARY) OVER () AS avg_salary,
DEPT_ID
FROM EMPLOYEE
)

SELECT EMP_NAME,
DEPT_ID,
EMP_SALARY
FROM CTE_EMP
WHERE EMP_SALARY > avg_salary
AND EMP_NO > 103;

LATEST HIRE

SELECT *
FROM EMPLOYEE
WHERE DEPT_ID = 123
ORDER BY hire_date DESC
LIMIT 1;

OR (alternative)

WITH EmployeeData AS (
SELECT *,
MAX(HIRE_DATE) OVER (PARTITION BY DEPT_ID) AS MaxHireDate
FROM EMPLOYEE
)
SELECT *
FROM EmployeeData
WHERE HIRE_DATE = MaxHireDate
AND DEPT_ID = 123;
kth LARGEST

import sys

def kthLargest(arr,k):

set1 = set(arr)
newlist = list(set1)
[Link]()

if len(newlist) >= k:
return newlist[-k]
else:
return -1

def main():
import ast
arr = []
arr = ast.literal_eval(input())

k = int(input())

result = kthLargest(arr,k)
print(result)

if __name__ = "__main__":
main()

PERFECT SQUARE:

import sys

def perfect_number(n):

total_sum = 0
for i in range(1,n):
if n % i == 0:
total_sum += i

if total_sum == n:
return 1
else:
return 0

COUNT ORDERS BY TYPE (SQL) – Maharashtra

SELECT count(*) AS oc, type


FROM orders
WHERE Order_State = 'Maharashtra'
GROUP BY type
ORDER BY oc ASC;

JOINS on MySQL

SELECT a.*
FROM orders a
INNER JOIN customer_info c ON a.customer_id = [Link]
WHERE [Link]='AZ' and [Link] LIKE '%Silver%'
ORDER BY a.order_id;
COMPANY and PRODUCT (SQL)

SELECT PROD_NAME, PROD_PRICE, COM_NAME


FROM PRODUCT
JOIN COMPANY USING (COM_ID)
ORDER BY PROD_PRICE DESC
LIMIT 1;

You might also like