Data Analysis Useful Resources
#dataanalysis
#dataanalysisbooks
#sqlbooks
#pythonbooks
#tableau
#powerbi
#datavisualization
For promotions: @coderfun
Post #1802
1.64K
DA @learndataanalysis
Showing posts older than #1803 · Back to latest
ROW_NUMBER(), RANK(), DENSE_RANK(), NTILE(), and LAG()/LEAD(). These functions allow you to perform calculations across a set of table rows related to the current row without collapsing the result set into a single output.WITH clause to create temporary result sets that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement. CTEs improve the readability and maintainability of complex queries.CLUSTERED versus NON-CLUSTERED indexes.EXPLAIN or EXPLAIN ANALYZE to identify performance bottlenecks and optimize query performance.AFTER INSERT, BEFORE UPDATE). Triggers are useful for maintaining data integrity and automating workflows.READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE) and their impact on data consistency and concurrency.PIVOT operator to transform row data into columnar data and UNPIVOT to convert columns back into rows. These operations are crucial for reshaping data for reporting and analysis.JSON_EXTRACT(), XMLQUERY(), or array functions.SELECT AVG(salary) AS average_salary FROM employees;
Find Median (Using Window Functions) SELECT salary FROM ( SELECT salary, ROW_NUMBER() OVER (ORDER BY salary) AS row_num, COUNT(*) OVER () AS total_rows FROM employees ) subquery WHERE row_num = (total_rows / 2);
SELECT department, COUNT(*) AS count FROM employees GROUP BY department ORDER BY count DESC LIMIT 1;
SELECT VARIANCE(salary) AS salary_variance, STDDEV(salary) AS salary_std_dev FROM employees;
df['salary'].mean() df['salary'].median() df['salary'].mode()[0]
df['salary'].var() df['salary'].std()
SELECT salary, COUNT(*) FROM employees GROUP BY salary ORDER BY salary;
import matplotlib.pyplot as plt
import seaborn as sns
df.groupby('category')['sales'].sum().plot(kind='bar')
plt.title('Total Sales by Category')
plt.xlabel('Category')
plt.ylabel('Sales')
plt.show()
sns.histplot(df['salary'], bins=10, kde=True)
plt.title('Salary Distribution')
plt.show()
sns.boxplot(y=df['sales'])
plt.title('Sales Data Outliers')
plt.show()
sns.heatmap(df.corr(), annot=True, cmap='coolwarm') plt.title('Feature Correlation Heatmap') plt.show() SELECT * FROM employees WHERE salary > (SELECT AVG(salary) + 2 * STDDEV(salary) FROM employees);
from scipy import stats df['z_score'] = stats.zscore(df['salary']) df_outliers = df[df['z_score'].abs() > 3]
Q1 = df['salary'].quantile(0.25)
Q3 = df['salary'].quantile(0.75)
IQR = Q3 - Q1
df_outliers = df[(df['salary'] < (Q1 - 1.5 * IQR)) | (df['salary'] > (Q3 + 1.5 * IQR))]


SELECT e.name, e.salary, d.department_name
FROM employees e
INNER JOIN departments d ON e.department = d.department_id;SELECT e.name, e.salary, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department = d.department_id;SELECT e.name, e.salary, d.department_name
FROM employees e
RIGHT JOIN departments d ON e.department = d.department_id;SELECT e.name, e.salary, d.department_name
FROM employees e
FULL OUTER JOIN departments d ON e.department = d.department_id;-- Find employees with salaries above the average salary of their department
SELECT name, salary
FROM employees e1
WHERE salary > (SELECT AVG(salary)
FROM employees e2
WHERE e1.department = e2.department);SELECT name,
(SELECT AVG(salary) FROM employees) AS avg_salary
FROM employees;-- Rank employees by salary within each department
SELECT name, department, salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
FROM employees;-- Calculate department-wise average salary using a CTE
WITH avg_salary_cte AS (
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
)
SELECT e.name, e.salary, a.avg_salary
FROM employees e
JOIN avg_salary_cte a ON e.department = a.department;-- Categorize employees based on salary
SELECT name,
CASE
WHEN salary < 50000 THEN 'Low'
WHEN salary BETWEEN 50000 AND 100000 THEN 'Medium'
ELSE 'High'
END AS salary_category
FROM employees;-- Concatenate first and last names
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
-- Trim extra spaces from a string
SELECT TRIM(name) FROM employees;-- Calculate tenure in days
SELECT name, DATEDIFF(CURDATE(), hire_date) AS days_tenure
FROM employees;-- Create an index on the department column for faster lookups
CREATE INDEX idx_department ON employees(department);-- Create a view for high-salary employees
CREATE VIEW high_salary_employees AS
SELECT name, salary
FROM employees
WHERE salary > 100000;
-- Query the view
SELECT * FROM high_salary_employees;-- -- Transaction example
START TRANSACTION;
UPDATE employees SET salary = salary + 5000 WHERE department = 'HR';
DELETE FROM employees WHERE id = 10;
COMMIT; -- Commit the transaction if all