SQL Programming Resources
75.7K subscribers
503 photos
13 files
442 links
Find top SQL resources from global universities, cool projects, and learning materials for data analytics.

Admin: @coderfun

Useful links: heylink.me/DataAnalytics

Promotions: @love_data
Download Telegram
Top 10 Advanced SQL Interview Questions and Answers 

1. What is a Common Table Expression (CTE), and when would you use it? 
   A Common Table Expression (CTE) is a temporary result set that can be referred to within a SELECT, INSERT, UPDATE, or DELETE statement. 
   Example: 
 
   WITH SalesCTE AS (
       SELECT SalespersonID, SUM(SalesAmount) AS TotalSales
       FROM Sales
       GROUP BY SalespersonID
   )
   SELECT * FROM SalesCTE WHERE TotalSales > 5000;
  


2. How do you optimize a query with a large dataset? 
   - Use proper indexes. 
   - Avoid SELECT *; only retrieve required columns. 
   - Break down complex queries using temporary tables or CTEs. 
   - Analyze query execution plans. 

3. What is the difference between RANK(), DENSE_RANK(), and ROW_NUMBER()? 
   - RANK(): Skips ranking if thereโ€™s a tie (e.g., 1, 2, 2, 4). 
   - DENSE_RANK(): Does not skip ranks after a tie (e.g., 1, 2, 2, 3). 
   - ROW_NUMBER(): Assigns unique numbers sequentially, regardless of ties. 

4. How do you find duplicate records in a table? 
 
   SELECT ColumnName, COUNT(*)
   FROM TableName
   GROUP BY ColumnName
   HAVING COUNT(*) > 1;
  


5. What is the difference between INNER JOIN and LEFT JOIN? 
   - INNER JOIN: Returns records that match in both tables. 
   - LEFT JOIN: Returns all records from the left table, and matching records from the right table (NULL if no match). 

6. Explain window functions and provide an example. 
   Window functions operate on a set of rows related to the current row, without collapsing them into a single output. 
   Example: 
 
   SELECT EmployeeID, Salary, 
          RANK() OVER (PARTITION BY DepartmentID ORDER BY Salary DESC) AS Rank
   FROM Employees;
  


7. What are the different types of indexes in SQL? 
   - Clustered Index: Reorders the data physically in the table. 
   - Non-Clustered Index: Creates a separate structure for data retrieval. 
   - Unique Index: Ensures no duplicate values in the column. 

8. How do you handle NULL values in SQL? 
   - Use COALESCE() or ISNULL() to replace NULL values. 
   - Filter with IS NULL or IS NOT NULL in WHERE clauses. 
   Example: 
 
   SELECT COALESCE(PhoneNumber, 'N/A') AS ContactNumber FROM Customers;
  


9. What is the difference between DELETE and TRUNCATE? 
   - DELETE: Removes specific rows, can use WHERE clause, and logs individual row deletions. 
   - TRUNCATE: Removes all rows, faster, and resets table identity. 

10. How do you use a CASE statement in SQL? 
  
    SELECT ProductName,
           CASE
               WHEN Quantity > 100 THEN 'High Stock'
               WHEN Quantity BETWEEN 50 AND 100 THEN 'Medium Stock'
               ELSE 'Low Stock'
           END AS StockStatus
    FROM Products;
   
๐Ÿ‘16โค3
SQL INTERVIEW Questions

Explain the concept of window functions in SQL. Provide examples to illustrate their usage.

Answer:

Window Functions:
Window functions perform calculations across a set of table rows related to the current row. Unlike aggregate functions, window functions do not group rows into a single output row; instead, they return a value for each row in the query result.

Types of Window Functions:
1. Aggregate Window Functions: Compute aggregate values like SUM, AVG, COUNT, etc.
2. Ranking Window Functions: Assign a rank to each row, such as RANK(), DENSE_RANK(), and ROW_NUMBER().
3. Analytic Window Functions: Perform calculations like LEAD(), LAG(), FIRST_VALUE(), and LAST_VALUE().

Syntax:
SELECT column_name, 
window_function() OVER (PARTITION BY column_name ORDER BY column_name)
FROM table_name;

Examples:

1. Using ROW_NUMBER():
Assign a unique number to each row within a partition of the result set.

   SELECT employee_name, department_id, salary,
ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank
FROM employees;

This query ranks employees within each department based on their salary in descending order.

2. Using AVG() with OVER():
Calculate the average salary within each department without collapsing the result set.

   SELECT employee_name, department_id, salary,
AVG(salary) OVER (PARTITION BY department_id) AS avg_salary
FROM employees;

This query returns the average salary for each department along with each employee's salary.

3. Using LEAD():
Access the value of a subsequent row in the result set.

   SELECT employee_name, department_id, salary,
LEAD(salary, 1) OVER (PARTITION BY department_id ORDER BY salary) AS next_salary
FROM employees;

This query retrieves the salary of the next employee within the same department based on the current sorting order.

4. Using RANK():
Assign a rank to each row within the partition, with gaps in the ranking values if there are ties.

   SELECT employee_name, department_id, salary,
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank
FROM employees;

This query ranks employees within each department by their salary in descending order, leaving gaps for ties.

Tip: Window functions are powerful for performing calculations across a set of rows while retaining the individual rows. They are useful for running totals, moving averages, ranking, and accessing data from other rows within the same result set.

Go though SQL Learning Series to refresh your basics

Share with credits: https://t.iss.one/sqlspecialist

Like this post if you want me to continue SQL Interview Preparation Series ๐Ÿ‘โค๏ธ

Hope it helps :)
๐Ÿ‘8โค2๐Ÿ‘1
If you want to be a data analyst, you should work to become as good at SQL as possible.

1. SELECT

What a surprise! I need to choose what data I want to return.

2. FROM

Again, no shock here. I gotta choose what table I am pulling my data from.

3. WHERE

This is also pretty basic, but I almost always filter the data to whatever range I need and filter the data to whatever condition Iโ€™m looking for.

4. JOIN

This may surprise you that the next one isnโ€™t one of the other core SQL clauses, but at least for my work, I utilize some kind of join in almost every query I write.

5. Calculations

This isnโ€™t necessarily a function of SQL, but I write a lot of calculations in my queries. Common examples include finding the time between two dates and multiplying and dividing values to get what I need.

Add operators and a couple data cleaning functions and thatโ€™s 80%+ of the SQL I write on the job.
โค6๐Ÿ‘4
As a Data_Analyst, SQL has been important l for conducting in-depth data analysis. Here are some advanced SQL techniques that can significantly enhance your analytical capabilities:

1. Window Functions:
โ€ข Advanced Analytics: Master the use of OVER() for complex analytical tasks. Window functions are essential for calculating running totals, rankings, and performing lead-lag analysis within datasets. Explore functions like ROW_NUMBER(), RANK(), DENSE_RANK(), and NTILE() to gain nuanced insights into your data.
โ€ข Partitioning and Ordering: Learn how to partition your data and order within partitions to perform segmented calculations efficiently.

2. CTEs and Temporary Tables:
โ€ข Simplifying Complex Queries: Common Table Expressions (CTEs) and temporary tables are invaluable for breaking down and simplifying complex queries, especially when dealing with large datasets.
โ€ข Recursive CTEs: Utilize recursive CTEs for hierarchical data processing and recursive algorithms, which can be critical for tasks like organizational chart creation and graph traversal.
โ€ข Performance Considerations: Understand when to use CTEs versus temporary tables for optimal performance and resource management.

3. Dynamic SQL:
โ€ข Flexibility and Responsiveness: Learn to construct SQL queries dynamically to enhance the flexibility of your database interactions. Dynamic SQL allows you to create more adaptable and responsive applications by building queries based on variable inputs and user interactions.
โ€ข Security Best Practices: Implement best practices for securing dynamic SQL, such as using parameterized queries to prevent SQL injection attacks.

4. Query Optimization:
โ€ข Performance Tuning: Delve into advanced techniques for optimizing query performance. This includes the strategic use of indexing, query restructuring, and understanding execution plans to significantly boost efficiency.
โ€ข Indexing Strategies: Explore different types of indexes (e.g., clustered, non-clustered, covering indexes) and their appropriate use cases.
โ€ข Execution Plans: Gain expertise in reading and interpreting execution plans to identify bottlenecks and optimize query performance.

5. PIVOT and UNPIVOT:
โ€ข Data Transformation: These operations are crucial for transforming rows into columns and vice versa, making your data more accessible and analysis-friendly.
โ€ข Advanced Pivoting: Combine PIVOT and UNPIVOT with aggregate functions to summarize data dynamically. This is particularly useful for creating cross-tab reports and reshaping data for better visualization and analysis.
โ€ข Complex Transformations: Implement complex data transformations using nested PIVOT/UNPIVOT operations to handle multi-dimensional data structures effectively.
๐Ÿ‘3โค1
30-day Roadmap plan for SQL covers beginner, intermediate, and advanced topics ๐Ÿ‘‡

Week 1: Beginner Level

Day 1-3: Introduction and Setup
1. Day 1: Introduction to SQL, its importance, and various database systems.
2. Day 2: Installing a SQL database (e.g., MySQL, PostgreSQL).
3. Day 3: Setting up a sample database and practicing basic commands.

Day 4-7: Basic SQL Queries
4. Day 4: SELECT statement, retrieving data from a single table.
5. Day 5: WHERE clause and filtering data.
6. Day 6: Sorting data with ORDER BY.
7. Day 7: Aggregating data with GROUP BY and using aggregate functions (COUNT, SUM, AVG).

Week 2-3: Intermediate Level

Day 8-14: Working with Multiple Tables
8. Day 8: Introduction to JOIN operations.
9. Day 9: INNER JOIN and LEFT JOIN.
10. Day 10: RIGHT JOIN and FULL JOIN.
11. Day 11: Subqueries and correlated subqueries.
12. Day 12: Creating and modifying tables with CREATE, ALTER, and DROP.
13. Day 13: INSERT, UPDATE, and DELETE statements.
14. Day 14: Understanding indexes and optimizing queries.

Day 15-21: Data Manipulation
15. Day 15: CASE statements for conditional logic.
16. Day 16: Using UNION and UNION ALL.
17. Day 17: Data type conversions (CAST and CONVERT).
18. Day 18: Working with date and time functions.
19. Day 19: String manipulation functions.
20. Day 20: Error handling with TRY...CATCH.
21. Day 21: Practice complex queries and data manipulation tasks.

Week 4: Advanced Level

Day 22-28: Advanced Topics
22. Day 22: Working with Views.
23. Day 23: Stored Procedures and Functions.
24. Day 24: Triggers and transactions.
25. Day 25: Windows Function

Day 26-30: Real-World Projects
26. Day 26: SQL Project-1
27. Day 27: SQL Project-2
28. Day 28: SQL Project-3
29. Day 29: Practice questions set
30. Day 30: Final review and practice, explore advanced topics in depth, or work on a personal project.

Like for more
๐Ÿ‘15
Top 10 SQL statements & functions used for data analysis

SELECT: To retrieve data from a database.
FROM: To specify the table or tables from which to retrieve data.
WHERE: To filter data based on specified conditions.
GROUP BY: To group rows with similar values into summary rows.
HAVING: To filter grouped data based on conditions.
ORDER BY: To sort the result set by one or more columns.
COUNT(): To count the number of rows or non-null values in a column.
SUM(): To calculate the sum of values in a numeric column.
AVG(): To calculate the average of values in a numeric column.
JOIN: To combine data from multiple tables based on a related column.

These SQL statements and functions are fundamental for data analysis and querying relational databases effectively.

Hope it helps :)
๐Ÿ‘11
๐‘ช๐’๐’Ž๐’‘๐’“๐’†๐’‰๐’†๐’๐’”๐’Š๐’—๐’† ๐’“๐’๐’‚๐’…๐’Ž๐’‚๐’‘ ๐’•๐’ ๐’ƒ๐’†๐’„๐’๐’Ž๐’Š๐’๐’ˆ ๐’‚ ๐’Ž๐’‚๐’”๐’•๐’†๐’“ ๐’Š๐’ ๐‘บ๐‘ธ๐‘ณ:

1. ๐‘ผ๐’๐’…๐’†๐’“๐’”๐’•๐’‚๐’๐’… ๐’•๐’‰๐’† ๐‘ฉ๐’‚๐’”๐’Š๐’„๐’” ๐’๐’‡ ๐‘บ๐‘ธ๐‘ณ

๐€. ๐ˆ๐ง๐ญ๐ซ๐จ๐๐ฎ๐œ๐ญ๐ข๐จ๐ง ๐ญ๐จ ๐ƒ๐š๐ญ๐š๐›๐š๐ฌ๐ž๐ฌ

๐–๐ก๐š๐ญ ๐ข๐ฌ ๐š ๐ƒ๐š๐ญ๐š๐›๐š๐ฌ๐ž?: Understanding the concept of databases and relational databases.

๐ƒ๐š๐ญ๐š๐›๐š๐ฌ๐ž ๐Œ๐š๐ง๐š๐ ๐ž๐ฆ๐ž๐ง๐ญ ๐’๐ฒ๐ฌ๐ญ๐ž๐ฆ๐ฌ (๐ƒ๐๐Œ๐’): Learn about different DBMS like MySQL, PostgreSQL, SQL Server, Oracle.

๐. ๐๐š๐ฌ๐ข๐œ ๐’๐๐‹ ๐‚๐จ๐ฆ๐ฆ๐š๐ง๐๐ฌ

๐ƒ๐š๐ญ๐š ๐‘๐ž๐ญ๐ซ๐ข๐ž๐ฏ๐š๐ฅ:
๐’๐„๐‹๐„๐‚๐“: Basic retrieval of data.
๐–๐‡๐„๐‘๐„: Filtering data based on conditions.
๐Ž๐‘๐ƒ๐„๐‘ ๐๐˜: Sorting results.
๐‹๐ˆ๐Œ๐ˆ๐“: Limiting the number of rows returned.

๐ƒ๐š๐ญ๐š ๐Œ๐š๐ง๐ข๐ฉ๐ฎ๐ฅ๐š๐ญ๐ข๐จ๐ง:
๐ˆ๐๐’๐„๐‘๐“: Adding new data.
๐”๐๐ƒ๐€๐“๐„: Modifying existing data.
๐ƒ๐„๐‹๐„๐“๐„: Removing data.

2. ๐ˆ๐ง๐ญ๐ž๐ซ๐ฆ๐ž๐๐ข๐š๐ญ๐ž ๐’๐๐‹ ๐’๐ค๐ข๐ฅ๐ฅ๐ฌ
๐€. ๐€๐๐ฏ๐š๐ง๐œ๐ž๐ ๐ƒ๐š๐ญ๐š ๐‘๐ž๐ญ๐ซ๐ข๐ž๐ฏ๐š๐ฅ

๐‰๐Ž๐ˆ๐๐ฌ: Understanding different types of joins (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN).
๐€๐ ๐ ๐ซ๐ž๐ ๐š๐ญ๐ž ๐…๐ฎ๐ง๐œ๐ญ๐ข๐จ๐ง๐ฌ: Using functions like COUNT, SUM, AVG, MIN, MAX.
๐†๐‘๐Ž๐”๐ ๐๐˜: Grouping data to perform aggregate calculations.
๐‡๐€๐•๐ˆ๐๐†: Filtering groups based on aggregate values.

๐. ๐’๐ฎ๐›๐ช๐ฎ๐ž๐ซ๐ข๐ž๐ฌ ๐š๐ง๐ ๐๐ž๐ฌ๐ญ๐ž๐ ๐๐ฎ๐ž๐ซ๐ข๐ž๐ฌ
๐’๐ฎ๐›๐ช๐ฎ๐ž๐ซ๐ข๐ž๐ฌ: Using queries within queries.
๐‚๐จ๐ซ๐ซ๐ž๐ฅ๐š๐ญ๐ž๐ ๐’๐ฎ๐›๐ช๐ฎ๐ž๐ซ๐ข๐ž๐ฌ: Subqueries that reference columns from the outer query.

๐‘ช. ๐‘ซ๐’‚๐’•๐’‚ ๐‘ซ๐’†๐’‡๐’Š๐’๐’Š๐’•๐’Š๐’๐’ ๐‘ณ๐’‚๐’๐’ˆ๐’–๐’‚๐’ˆ๐’† (๐‘ซ๐‘ซ๐‘ณ)
๐‚๐ซ๐ž๐š๐ญ๐ข๐ง๐  ๐“๐š๐›๐ฅ๐ž๐ฌ: CREATE TABLE.
๐Œ๐จ๐๐ข๐Ÿ๐ฒ๐ข๐ง๐  ๐“๐š๐›๐ฅ๐ž๐ฌ: ALTER TABLE.
๐‘น๐’†๐’Ž๐’๐’—๐’Š๐’๐’ˆ ๐‘ป๐’‚๐’ƒ๐’๐’†๐’”: DROP TABLE.

3. ๐€๐๐ฏ๐š๐ง๐œ๐ž๐ ๐’๐๐‹ ๐“๐ž๐œ๐ก๐ง๐ข๐ช๐ฎ๐ž๐ฌ
๐€. ๐๐ž๐ซ๐Ÿ๐จ๐ซ๐ฆ๐š๐ง๐œ๐ž ๐Ž๐ฉ๐ญ๐ข๐ฆ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง
๐ˆ๐ง๐๐ž๐ฑ๐ž๐ฌ: Understanding and creating indexes to speed up queries.
๐๐ฎ๐ž๐ซ๐ฒ ๐Ž๐ฉ๐ญ๐ข๐ฆ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง: Techniques to write efficient SQL queries.

๐. ๐€๐๐ฏ๐š๐ง๐œ๐ž๐ ๐’๐๐‹ ๐…๐ฎ๐ง๐œ๐ญ๐ข๐จ๐ง๐ฌ
๐–๐ข๐ง๐๐จ๐ฐ ๐…๐ฎ๐ง๐œ๐ญ๐ข๐จ๐ง๐ฌ: Using functions like ROW_NUMBER, RANK, DENSE_RANK, LEAD, LAG.
๐‚๐“๐„ (๐‚๐จ๐ฆ๐ฆ๐จ๐ง ๐“๐š๐›๐ฅ๐ž ๐„๐ฑ๐ฉ๐ซ๐ž๐ฌ๐ฌ๐ข๐จ๐ง๐ฌ): Using WITH to create temporary result sets.

๐‚. ๐“๐ซ๐š๐ง๐ฌ๐š๐œ๐ญ๐ข๐จ๐ง๐ฌ ๐š๐ง๐ ๐‚๐จ๐ง๐œ๐ฎ๐ซ๐ซ๐ž๐ง๐œ๐ฒ
๐“๐ซ๐š๐ง๐ฌ๐š๐œ๐ญ๐ข๐จ๐ง๐ฌ: Using BEGIN, COMMIT, ROLLBACK.
๐‚๐จ๐ง๐œ๐ฎ๐ซ๐ซ๐ž๐ง๐œ๐ฒ ๐‚๐จ๐ง๐ญ๐ซ๐จ๐ฅ: Understanding isolation levels and locking mechanisms.

4. ๐๐ซ๐š๐œ๐ญ๐ข๐œ๐š๐ฅ ๐€๐ฉ๐ฉ๐ฅ๐ข๐œ๐š๐ญ๐ข๐จ๐ง๐ฌ ๐š๐ง๐ ๐‘๐ž๐š๐ฅ-๐–๐จ๐ซ๐ฅ๐ ๐’๐œ๐ž๐ง๐š๐ซ๐ข๐จ๐ฌ
๐€. ๐ƒ๐š๐ญ๐š๐›๐š๐ฌ๐ž ๐ƒ๐ž๐ฌ๐ข๐ ๐ง
๐๐จ๐ซ๐ฆ๐š๐ฅ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง: Understanding normal forms and how to normalize databases.
๐„๐‘ ๐ƒ๐ข๐š๐ ๐ซ๐š๐ฆ๐ฌ: Creating Entity-Relationship diagrams to model databases.

๐. ๐ƒ๐š๐ญ๐š ๐ˆ๐ง๐ญ๐ž๐ ๐ซ๐š๐ญ๐ข๐จ๐ง
๐„๐“๐‹ ๐๐ซ๐จ๐œ๐ž๐ฌ๐ฌ๐ž๐ฌ: Extract, Transform, Load processes for data integration.

๐’๐ญ๐จ๐ซ๐ž๐ ๐๐ซ๐จ๐œ๐ž๐๐ฎ๐ซ๐ž๐ฌ ๐š๐ง๐ ๐“๐ซ๐ข๐ ๐ ๐ž๐ซ๐ฌ: Writing and using stored procedures and triggers for complex logic and automation.

๐‚. ๐‚๐š๐ฌ๐ž ๐’๐ญ๐ฎ๐๐ข๐ž๐ฌ ๐š๐ง๐ ๐๐ซ๐จ๐ฃ๐ž๐œ๐ญ๐ฌ
๐‘๐ž๐š๐ฅ-๐–๐จ๐ซ๐ฅ๐ ๐’๐œ๐ž๐ง๐š๐ซ๐ข๐จ๐ฌ: Work on case studies involving complex database operations.

๐‚๐š๐ฉ๐ฌ๐ญ๐จ๐ง๐ž ๐๐ซ๐จ๐ฃ๐ž๐œ๐ญ๐ฌ: Develop comprehensive projects that showcase your SQL expertise.

๐‘๐ž๐ฌ๐จ๐ฎ๐ซ๐œ๐ž๐ฌ ๐š๐ง๐ ๐“๐จ๐จ๐ฅ๐ฌ
๐๐จ๐จ๐ค๐ฌ: "SQL in 10 Minutes, Sams Teach Yourself" by Ben Forta, "SQL for Data Scientists" by Renee M. P. Teate.
๐Ž๐ง๐ฅ๐ข๐ง๐ž ๐๐ฅ๐š๐ญ๐Ÿ๐จ๐ซ๐ฆ๐ฌ: Coursera, Udacity, edX, Khan Academy.
๐๐ซ๐š๐œ๐ญ๐ข๐œ๐ž ๐๐ฅ๐š๐ญ๐Ÿ๐จ๐ซ๐ฆ๐ฌ: LeetCode, HackerRank, Mode Analytics, SQLZoo.
๐Ÿ‘8โค5
Top 10 SQL concepts for data analysts

1. Database: A database is a structured collection of data that is organized and managed for easy access and retrieval. It stores data in tables, which are made up of rows and columns.

2. SQL (Structured Query Language): SQL is a programming language used to communicate with and manipulate databases. It allows data analysts to retrieve, insert, update, and delete data from a database.

3. Tables: Tables are the basic building blocks of a database. They consist of rows (records) and columns (fields) that store data in a structured format.

4. Queries: Queries are SQL statements used to retrieve specific data from a database. They can be simple or complex, involving multiple tables and conditions.

5. Joins: Joins are used to combine data from multiple tables based on a common field. They allow data analysts to retrieve related information from different tables in a single query.

6. Aggregation Functions: Aggregation functions, such as SUM, COUNT, AVG, MIN, and MAX, are used to perform calculations on groups of data. They are commonly used to summarize and analyze large datasets.

7. Filtering: Filtering allows data analysts to retrieve specific data based on certain conditions. It involves using the WHERE clause in SQL queries to specify the criteria for selecting records.

8. Sorting: Sorting allows data analysts to arrange data in a specific order. It involves using the ORDER BY clause in SQL queries to sort records based on one or more columns.

9. Subqueries: Subqueries are queries nested within another query. They allow data analysts to perform complex operations by using the results of one query as input for another query.

10. Data Manipulation Language (DML): DML statements, such as INSERT, UPDATE, and DELETE, are used to modify data in a database. Data analysts use these statements to insert new records, update existing records, or delete unwanted records.
๐Ÿ‘8
Execution order in SQL ๐Ÿ‘†
โค20๐Ÿ‘6
SQL Interview Questions !!

๐ŸŽ— Write a query to find all employees whose salaries exceed the company's average salary.
๐ŸŽ— Write a query to retrieve the names of employees who work in the same department as 'John Doe'.
๐ŸŽ— Write a query to display the second highest salary from the Employee table without using the MAX function twice.
๐ŸŽ— Write a query to find all customers who have placed more than five orders.
๐ŸŽ— Write a query to count the total number of orders placed by each customer.
๐ŸŽ— Write a query to list employees who joined the company within the last 6 months.
๐ŸŽ— Write a query to calculate the total sales amount for each product.
๐ŸŽ— Write a query to list all products that have never been sold.
๐ŸŽ— Write a query to remove duplicate rows from a table.
๐ŸŽ— Write a query to identify the top 10 customers who have not placed any orders in the past year.

Here you can find more SQL Resources๐Ÿ‘‡
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v

Hope it helps :)
โค4๐Ÿ‘1
SQL Interview Questions which can be asked in a Data Analyst Interview.

1๏ธโƒฃ What is difference between Primary key and Unique key?
       
โ—ผPrimary key- A column or set of columns which uniquely identifies each record in a table. It can't contain null values and only one primary key
can exist in a table.

โ—ผUnique key-Similar to primary key it also uniquely identifies each record in a table and can contain null values.Multiple Unique key can exist in a table.

2๏ธโƒฃ What is a Candidate key?

โ—ผA key or set of keys that uniquely identifies each record in a table.It is a combination of Primary and Alternate key.

3๏ธโƒฃ What is a Constraint?

โ—ผSpecific rule or limit that we define in our table. E.g - NOT NULL,AUTO INCREMENT

4๏ธโƒฃ Can you differentiate between TRUNCATE and DELETE?

โ—ผTRUNCATE is a DDL command. It deletes the entire data from a table but preserves the structure of table.It doesn't deletes the data row by row hence faster than DELETE command, while DELETE is a DML command and it deletes the entire data based on specified condition else deletes the entire data,also it deletes the data row by row hence slower than TRUNCATE command.

5๏ธโƒฃ What is difference between 'View' and 'Stored Procedure'?

โ—ผA View is a virtual table that gets data from the base table .It is basically a Select statement,while Stored Procedure is a sql statement or set of sql statement stored on database server.

6๏ธโƒฃ What is difference between a Common Table Expression and temporary table?

โ—ผCTE is a temporary result set that is defined within execution scope of a single SELECT ,DELETE,UPDATE statement while temporary table is stored in TempDB and gets deleted  once the session expires.

7๏ธโƒฃ Differentiate between a clustered index and a non-clustered index?

โ—ผ A clustered index determines physical ordering of data in a table and a table can have only one clustered index while a non-clustered index is analogous to index of a book where index is stored at one place and data at other place and index will have pointers to storage location of the data,a table can have more than one non-clustered index.

8๏ธโƒฃ Explain triggers ?

โ—ผThey are sql codes which are automatically executed in response to certain events on a table.They are used to maintain integrity of data.

Join our WhatsApp channel for more resources ๐Ÿ‘‡
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v

Like this post if you need more ๐Ÿ‘โค๏ธ

Hope it helps :)
โค3๐Ÿ‘1
Forwarded from Data Analytics
Which of the following is SQL Command is used to sort results?
Anonymous Quiz
34%
SORT BY
57%
ORDER BY
7%
SORTED
3%
ORDER ON
๐Ÿ‘5
Many people pay too much to learn SQL, but my mission is to break down barriers. I have shared complete learning series to learn SQL from scratch.

Here are the links to the SQL series

Complete SQL Topics for Data Analyst: https://t.iss.one/sqlspecialist/523

Part-1: https://t.iss.one/sqlspecialist/524

Part-2: https://t.iss.one/sqlspecialist/525

Part-3: https://t.iss.one/sqlspecialist/526

Part-4: https://t.iss.one/sqlspecialist/527

Part-5: https://t.iss.one/sqlspecialist/529

Part-6: https://t.iss.one/sqlspecialist/534

Part-7: https://t.iss.one/sqlspecialist/534

Part-8: https://t.iss.one/sqlspecialist/536

Part-9: https://t.iss.one/sqlspecialist/537

Part-10: https://t.iss.one/sqlspecialist/539

Part-11: https://t.iss.one/sqlspecialist/540

Part-12:
https://t.iss.one/sqlspecialist/541

Part-13: https://t.iss.one/sqlspecialist/542

Part-14: https://t.iss.one/sqlspecialist/544

Part-15: https://t.iss.one/sqlspecialist/545

Part-16: https://t.iss.one/sqlspecialist/546

Part-17: https://t.iss.one/sqlspecialist/549

Part-18: https://t.iss.one/sqlspecialist/552

Part-19: https://t.iss.one/sqlspecialist/555

Part-20: https://t.iss.one/sqlspecialist/556

I saw a lot of big influencers copy pasting my content after removing the credits. It's absolutely fine for me as more people are getting free education because of my content.

But I will really appreciate if you share credits for the time and efforts I put in to create such valuable content. I hope you can understand.

Complete Python Topics for Data Analysts: https://t.iss.one/sqlspecialist/548

Complete Excel Topics for Data Analysts: https://t.iss.one/sqlspecialist/547

I'll continue with learning series on Python, Power BI, Excel & Tableau.

Thanks to all who support our channel and share the content with proper credits. You guys are really amazing.

Hope it helps :)
โค9๐Ÿ‘4๐Ÿ‘3
โœ๏ธBest practices for writing SQL ๐Ÿ“Šqueries:

1- Write SQL keywords in capital letters.

2- Use table aliases with columns when you are joining multiple tables.

3- Never use select *, always mention list of columns in select clause.

4- Add useful comments wherever you write complex logic. Avoid too many comments.

5- Use joins instead of subqueries when possible for better performance.

6- Create CTEs instead of multiple sub queries , it will make your query easy to read.

7- Join tables using JOIN keywords instead of writing join condition in where clause for better readability.

8- Never use order by in sub queries , It will unnecessary increase runtime.

9- If you know there are no duplicates in 2 tables, use UNION ALL instead of UNION for better performance.

Here you can find essential SQL Resources๐Ÿ‘‡
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v

Like this post if you need more ๐Ÿ‘โค๏ธ

Hope it helps :)
๐Ÿ‘7โค2
SQL Basics for Beginners: Must-Know Concepts

1. What is SQL? 
   SQL (Structured Query Language) is a standard language used to communicate with databases. It allows you to query, update, and manage relational databases by writing simple or complex queries.

2. SQL Syntax 
   SQL is written using statements, which consist of keywords like SELECT, FROM, WHERE, etc., to perform operations on the data.
   - SQL keywords are not case-sensitive, but it's common to write them in uppercase (e.g., SELECT, FROM).

3. SQL Data Types 
   Databases store data in different formats. The most common data types are:
   - INT (Integer): For whole numbers.
   - VARCHAR(n) or TEXT: For storing text data.
   - DATE: For dates.
   - DECIMAL: For precise decimal values, often used in financial calculations.

4. Basic SQL Queries 
   Here are some fundamental SQL operations:

   - SELECT Statement: Used to retrieve data from a database.
   
     SELECT column1, column2 FROM table_name;
    

   - WHERE Clause: Filters data based on conditions.
   
     SELECT * FROM table_name WHERE condition;
    

   - ORDER BY: Sorts data in ascending (ASC) or descending (DESC) order.
   
     SELECT column1, column2 FROM table_name ORDER BY column1 ASC;
    

   - LIMIT: Limits the number of rows returned.
   
     SELECT * FROM table_name LIMIT 5;
    

5. Filtering Data with WHERE Clause 
   The WHERE clause helps you filter data based on a condition:
 
   SELECT * FROM employees WHERE salary > 50000;
  

   You can use comparison operators like:
   - =: Equal to
   - >: Greater than
   - <: Less than
   - LIKE: For pattern matching

6. Aggregating Data 
   SQL provides functions to summarize or aggregate data:
   - COUNT(): Counts the number of rows.
   
     SELECT COUNT(*) FROM table_name;
    

   - SUM(): Adds up values in a column.
   
     SELECT SUM(salary) FROM employees;
    

   - AVG(): Calculates the average value.
   
     SELECT AVG(salary) FROM employees;
    

   - GROUP BY: Groups rows that have the same values into summary rows.
   
     SELECT department, AVG(salary) FROM employees GROUP BY department;
    

7. Joins in SQL 
   Joins combine data from two or more tables:
   - INNER JOIN: Retrieves records with matching values in both tables.
   
     SELECT employees.name, departments.department
     FROM employees
     INNER JOIN departments
     ON employees.department_id = departments.id;
    

   - LEFT JOIN: Retrieves all records from the left table and matched records from the right table.
   
     SELECT employees.name, departments.department
     FROM employees
     LEFT JOIN departments
     ON employees.department_id = departments.id;
    

8. Inserting Data
   To add new data to a table, you use the INSERT INTO statement:
 
   INSERT INTO employees (name, position, salary) VALUES ('John Doe', 'Analyst', 60000);
  

9. Updating Data
   You can update existing data in a table using the UPDATE statement:
 
   UPDATE employees SET salary = 65000 WHERE name = 'John Doe';
  

10. Deleting Data
    To remove data from a table, use the DELETE statement:
  
    DELETE FROM employees WHERE name = 'John Doe';
   


Hope it helps :)
๐Ÿ‘12โค10
๐Ÿ˜‚๐Ÿ˜‚
๐Ÿคฃ15โค5
SQL Programming Resources
๐Ÿ˜‚๐Ÿ˜‚
Jokes apart, if you really want to improve your communication skills, you should definitely join @englishlearnerspro
โค2๐Ÿ‘2
๐Ÿฑ ๐—ฆ๐—ค๐—Ÿ ๐— ๐˜†๐˜๐—ต๐˜€ ๐——๐—ฒ๐—ฏ๐˜‚๐—ป๐—ธ๐—ฒ๐—ฑ โŒ ๐—ช๐—ต๐—ฎ๐˜ ๐—•๐—ฒ๐—ด๐—ถ๐—ป๐—ป๐—ฒ๐—ฟ๐˜€ ๐—ข๐—ณ๐˜๐—ฒ๐—ป ๐—š๐—ฒ๐˜ ๐—ช๐—ฟ๐—ผ๐—ป๐—ด

SQL is super powerful, but some myths around it can trip up beginners.

Letโ€™s clear up five common misunderstandings and set the record straight:

๐— ๐˜†๐˜๐—ต ๐Ÿญ: ๐—ฆ๐—ค๐—Ÿ ๐—ถ๐˜€ ๐—ท๐˜‚๐˜€๐˜ ๐—ณ๐—ผ๐—ฟ ๐—ฝ๐˜‚๐—น๐—น๐—ถ๐—ป๐—ด ๐—ฑ๐—ฎ๐˜๐—ฎ.

โœฆ ๐—ฅ๐—ฒ๐—ฎ๐—น๐—ถ๐˜๐˜†: Nope, itโ€™s not just for that! SQL can also create, modify, and manage databases, control access, and maintain data consistency.

โœฆ ๐—™๐—ถ๐˜… ๐—ถ๐˜: Explore all the features of SQL, like DDL (for database design), DCL (for access control), and TCL (for transactions). It's more than just SELECT!

๐— ๐˜†๐˜๐—ต ๐Ÿฎ: ๐—จ๐˜€๐—ถ๐—ป๐—ด ๐—ฆ๐—˜๐—Ÿ๐—˜๐—–๐—ง *  ๐—ถ๐˜€ ๐—ณ๐—ถ๐—ป๐—ฒ.

โœฆ ๐—ฅ๐—ฒ๐—ฎ๐—น๐—ถ๐˜๐˜†: It might be easy, but itโ€™s not efficient. Pulling all columns wastes memory and slows down performance.

โœฆ ๐—™๐—ถ๐˜… ๐—ถ๐˜: Only select the columns you actually need. Itโ€™s faster and cleaner.
Not great - SELECT * FROM employees;
Better - SELECT employee_id, name, department FROM employees;

๐— ๐˜†๐˜๐—ต ๐Ÿฏ: ๐—ฆ๐—ค๐—Ÿ ๐—ฐ๐—ฎ๐—ป'๐˜ ๐—ต๐—ฎ๐—ป๐—ฑ๐—น๐—ฒ ๐—ฐ๐—ผ๐—บ๐—ฝ๐—น๐—ฒ๐˜… ๐—ฎ๐—ป๐—ฎ๐—น๐˜†๐˜€๐—ถ๐˜€.

โœฆ ๐—ฅ๐—ฒ๐—ฎ๐—น๐—ถ๐˜๐˜†: SQL can do way more than basic queries! With concepts like window functions and CTEs, you can handle really complex data analysis.

โœฆ ๐—™๐—ถ๐˜… ๐—ถ๐˜: Learn advanced SQL features like window functions (ROW_NUMBER(), RANK()) and CTEs to up your game.
Example - Ranking employees by salary within their department


WITH ranked_salaries AS (SELECT employee_id, salary, department,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank FROM employees)
SELECT * FROM ranked_salaries WHERE rank = 1;


๐— ๐˜†๐˜๐—ต ๐Ÿฐ: ๐—ฆ๐—น๐—ผ๐˜„ ๐—พ๐˜‚๐—ฒ๐—ฟ๐—ถ๐—ฒ๐˜€ ๐—ฎ๐—ฟ๐—ฒ ๐—ฎ๐—น๐˜„๐—ฎ๐˜†๐˜€ ๐˜๐—ต๐—ฒ ๐—ฑ๐—ฎ๐˜๐—ฎ๐—ฏ๐—ฎ๐˜€๐—ฒโ€™๐˜€ ๐—ณ๐—ฎ๐˜‚๐—น๐˜.

โœฆ ๐—ฅ๐—ฒ๐—ฎ๐—น๐—ถ๐˜๐˜†: Itโ€™s usually inefficient queries causing the slowdown. Things like missing indexes or unoptimized code can be the culprit.

โœฆ ๐—™๐—ถ๐˜… ๐—ถ๐˜: Use indexes properly, avoid complex calculations in WHERE clauses, and check your query execution plan to spot bottlenecks.

๐— ๐˜†๐˜๐—ต ๐Ÿฑ: ๐—ฆ๐—ค๐—Ÿ ๐—ถ๐˜€ ๐—ผ๐˜‚๐˜๐—ฑ๐—ฎ๐˜๐—ฒ๐—ฑ ๐—ฎ๐—ป๐—ฑ ๐˜„๐—ถ๐—น๐—น ๐—ฏ๐—ฒ ๐—ฟ๐—ฒ๐—ฝ๐—น๐—ฎ๐—ฐ๐—ฒ๐—ฑ ๐˜€๐—ผ๐—ผ๐—ป.

โœฆ ๐—ฅ๐—ฒ๐—ฎ๐—น๐—ถ๐˜๐˜†: SQL is here to stay! Despite the rise of NoSQL, SQL remains the backbone for structured data.

โœฆ ๐—™๐—ถ๐˜… ๐—ถ๐˜: Stay current and explore how SQL integrates with big data platforms and cloud databases. Itโ€™s more relevant than ever.

Donโ€™t let these myths hold you back. SQL is powerful, and when you understand it fully, you can do amazing things with your data.

Here you can find more SQL Resources
๐Ÿ‘‡๐Ÿ‘‡
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v

Like this post if you need more ๐Ÿ‘โค๏ธ

Hope it helps :)
๐Ÿ‘8โค1
Most Asked SQL Interview Questions at MAANG Companies๐Ÿ”ฅ๐Ÿ”ฅ

Preparing for an SQL Interview at MAANG Companies? Here are some crucial SQL Questions you should be ready to tackle:

1. How do you retrieve all columns from a table?

SELECT * FROM table_name;

2. What SQL statement is used to filter records?

SELECT * FROM table_name
WHERE condition;

The WHERE clause is used to filter records based on a specified condition.

3. How can you join multiple tables? Describe different types of JOINs.

SELECT columns
FROM table1
JOIN table2 ON table1.column = table2.column
JOIN table3 ON table2.column = table3.column;

Types of JOINs:

1. INNER JOIN: Returns records with matching values in both tables

SELECT * FROM table1
INNER JOIN table2 ON table1.column = table2.column;

2. LEFT JOIN: Returns all records from the left table & matched records from the right table. Unmatched records will have NULL values.

SELECT * FROM table1
LEFT JOIN table2 ON table1.column = table2.column;

3. RIGHT JOIN: Returns all records from the right table & matched records from the left table. Unmatched records will have NULL values.

SELECT * FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;

4. FULL JOIN: Returns records when there is a match in either left or right table. Unmatched records will have NULL values.

SELECT * FROM table1
FULL JOIN table2 ON table1.column = table2.column;

4. What is the difference between WHERE & HAVING clauses?

WHERE: Filters records before any groupings are made.

SELECT * FROM table_name
WHERE condition;

HAVING: Filters records after groupings are made.

SELECT column, COUNT(*)
FROM table_name
GROUP BY column
HAVING COUNT(*) > value;

5. How do you calculate average, sum, minimum & maximum values in a column?

Average: SELECT AVG(column_name) FROM table_name;

Sum: SELECT SUM(column_name) FROM table_name;

Minimum: SELECT MIN(column_name) FROM table_name;

Maximum: SELECT MAX(column_name) FROM table_name;

Here you can find essential SQL Interview Resources๐Ÿ‘‡
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v

Like this post if you need more ๐Ÿ‘โค๏ธ

Hope it helps :)
๐Ÿ‘8๐Ÿ‘2โค1
SQL, or Structured Query Language, is a domain-specific language used to manage and manipulate relational databases. Here's a brief A-Z overview by @sqlanalyst

A - Aggregate Functions: Functions like COUNT, SUM, AVG, MIN, and MAX used to perform operations on data in a database.

B - BETWEEN: A SQL operator used to filter results within a specific range.

C - CREATE TABLE: SQL statement for creating a new table in a database.

D - DELETE: SQL statement used to delete records from a table.

E - EXISTS: SQL operator used in a subquery to test if a specified condition exists.

F - FOREIGN KEY: A field in a database table that is a primary key in another table, establishing a link between the two tables.

G - GROUP BY: SQL clause used to group rows that have the same values in specified columns.

H - HAVING: SQL clause used in combination with GROUP BY to filter the results.

I - INNER JOIN: SQL clause used to combine rows from two or more tables based on a related column between them.

J - JOIN: Combines rows from two or more tables based on a related column.

K - KEY: A field or set of fields in a database table that uniquely identifies each record.

L - LIKE: SQL operator used in a WHERE clause to search for a specified pattern in a column.

M - MODIFY: SQL command used to modify an existing database table.

N - NULL: Represents missing or undefined data in a database.

O - ORDER BY: SQL clause used to sort the result set in ascending or descending order.

P - PRIMARY KEY: A field in a table that uniquely identifies each record in that table.

Q - QUERY: A request for data from a database using SQL.

R - ROLLBACK: SQL command used to undo transactions that have not been saved to the database.

S - SELECT: SQL statement used to query the database and retrieve data.

T - TRUNCATE: SQL command used to delete all records from a table without logging individual row deletions.

U - UPDATE: SQL statement used to modify the existing records in a table.

V - VIEW: A virtual table based on the result of a SELECT query.

W - WHERE: SQL clause used to filter the results of a query based on a specified condition.

X - (E)XISTS: Used in conjunction with SELECT to test the existence of rows returned by a subquery.

Z - ZERO: Represents the absence of a value in numeric fields or the initial state of boolean fields.

SQL Interview Resources๐Ÿ‘‡
https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v

Like this post if you need more ๐Ÿ‘โค๏ธ

Hope it helps :)
โค5๐Ÿ‘5