Perfect channel to learn Data Analytics
Learn SQL, Python, Alteryx, Tableau, Power BI and many more
For Promotions: @coderfun @love_data
Post #2710
6.9K
- ❤ 3
- 😁 1
DA @sqlspecialist
Showing posts older than #2711 · Back to latest
SELECT
name,
department,
salary,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank
FROM employees;
DENSE_RANK() window function partitioned by department and ordered by descending salary to assign ranks within each department. Unlike ROW_NUMBER(), DENSE_RANK() handles ties by assigning the same rank without gaps. This is ideal for leaderboards or performance analytics.SELECT
department,
MAX(salary) AS second_highest_salary
FROM (
SELECT
department,
salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) as rn
FROM employees
) ranked
WHERE rn = 2
GROUP BY department;
CREATE DATABASE db_name;USE db_name;CREATE TABLE table_name (col1 datatype, col2 datatype);DROP TABLE table_name;ALTER TABLE table_name ADD column_name datatype;INSERT INTO table_name (col1, col2) VALUES (val1, val2);SELECT * FROM table_name;SELECT col1, col2 FROM table_name;SELECT * FROM table_name WHERE condition;UPDATE table_name SET col1 = value1 WHERE condition;DELETE FROM table_name WHERE condition;SELECT * FROM table1 INNER JOIN table2 ON table1.col = table2.col;SELECT * FROM table1 LEFT JOIN table2 ON table1.col = table2.col;SELECT * FROM table1 RIGHT JOIN table2 ON table1.col = table2.col;SELECT COUNT(*) FROM table_name;SELECT SUM(col) FROM table_name;SELECT col, COUNT(*) FROM table_name GROUP BY col;SELECT * FROM table_name ORDER BY col ASC|DESC;SELECT * FROM table_name LIMIT n;CREATE INDEX idx_name ON table_name (col);DROP INDEX idx_name;SELECT * FROM table_name WHERE col IN (SELECT col FROM other_table);CREATE VIEW view_name AS SELECT * FROM table_name;DROP VIEW view_name;SELECT
category,
SUM(quantity * unit_price) AS total_revenue
FROM customer_orders
GROUP BY category;
SELECT
customer_id,
COUNT(order_id) AS order_count,
SUM(quantity * unit_price) AS total_spent
FROM customer_orders
GROUP BY customer_id
HAVING COUNT(order_id) > 1;
x = 10 y = "Hello"
x = 10
- Floats: y = 3.14
- Strings: name = "Alice"
- Lists: my_list = [1, 2, 3]
- Dictionaries: my_dict = {"key": "value"}
- Tuples: my_tuple = (1, 2, 3)
if, elif, else statementsfor i in range(5):
print(i)
while x < 5:
print(x)
x += 1
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
arr = np.array([1, 2, 3, 4])
arr.sum()
arr.mean()
arr.reshape((2, 2))
arr[0:2] # First two elements
df = pd.DataFrame({
'col1': [1, 2, 3],
'col2': ['A', 'B', 'C']
})
df = pd.read_csv('file.csv')
df.head() # First 5 rows
df.describe() # Summary statistics
df.info() # DataFrame info
df['col1']
df[['col1', 'col2']]
df[df['col1'] > 2]
df.dropna() # Drop missing values
df.fillna(0) # Replace missing values
df.groupby('col2').mean()
plt.plot(df['col1'], df['col2'])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Title')
plt.show()
sns.histplot(df['col1'])
sns.boxplot(x='col1', y='col2', data=df)
pd.merge(df1, df2, on='key')
df.pivot_table(index='col1', columns='col2', values='col3')
df['col1'].apply(lambda x: x*2)
df['col1'].mean()
df['col1'].median()
df['col1'].std()
df.corr()
id name department salary manager_id
1 Aditi HR 30000 5
2 Rahul IT 50000 6
3 Neha IT 60000 6
4 Aman Sales 40000 7
5 Kiran HR 70000 NULL
6 Mohit IT 80000 NULL
7 Suresh Sales 65000 NULL
8 Pooja HR 30000 5
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
SELECT name, department, salary
FROM employees e
WHERE salary > (
SELECT AVG(salary)
FROM employees
WHERE department = e.department
);
SELECT department, MAX(salary) AS max_salary
FROM employees
GROUP BY department;
SELECT e.name
FROM employees e
JOIN employees m ON e.manager_id = m.id
WHERE e.salary > m.salary;
SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department;
SELECT department, COUNT(*) AS total
FROM employees
GROUP BY department
HAVING COUNT(*) > 2;
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
SELECT name
FROM employees
WHERE manager_id IS NULL;
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;
SELECT salary, COUNT(*)
FROM employees
GROUP BY salary
HAVING COUNT(*) > 1;
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 2;