SQL Programming Resources
75.9K subscribers
511 photos
13 files
454 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
Best practices for writing SQL queries:

1- Filter Early, Aggregate Late: Apply filtering conditions in the WHERE clause early in the query, and perform aggregations in the HAVING or SELECT clauses as needed.

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

3- Never use select *, always mention list of columns in select clause before deploying the code.

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

5- Use joins instead of correlated 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. In fact some databases don't even allow you to do that.

9- If you know there are no duplicates in 2 tables, use UNION ALL instead of UNION for better performance.
โค4๐Ÿ‘3๐Ÿค”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;


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

Hope it helps :)
๐Ÿ‘12โค2
SQL Roadmap โœ…
๐Ÿ‘9โค5
Commit and master ๐—ฆ๐—ค๐—Ÿ in just ๐Ÿฏ๐Ÿฌ ๐——๐—ฎ๐˜†๐˜€!

I've outlined a simple, actionable plan for you to followโ€ฆ

๐—ช๐—ฒ๐—ฒ๐—ธ ๐Ÿญ: ๐—•๐—ฎ๐˜€๐—ถ๐—ฐ๐˜€ ๐—ผ๐—ณ ๐—ฆ๐—ค๐—Ÿ

โž› Day 1-2: Introduction to SQL, setting up your environment (MySQL/PostgreSQL/SQL Server).

โž› Day 3-4: Understanding databases, tables, and basic SQL syntax.

โž› Day 5-7: Working with SELECT, WHERE, and filtering data.

๐—ช๐—ฒ๐—ฒ๐—ธ ๐Ÿฎ: ๐—–๐—ผ๐—ฟ๐—ฒ ๐—ค๐˜‚๐—ฒ๐—ฟ๐—ถ๐—ฒ๐˜€

โž› Day 8-10: Using JOINs โ€“ INNER, LEFT, RIGHT, FULL.

โž› Day 11-13: GROUP BY, HAVING, and aggregate functions (SUM, COUNT, AVG).

โž› Day 14: Practice session โ€“ write complex queries.

๐—ช๐—ฒ๐—ฒ๐—ธ ๐Ÿฏ: ๐— ๐—ผ๐—ฑ๐—ถ๐—ณ๐˜†๐—ถ๐—ป๐—ด ๐——๐—ฎ๐˜๐—ฎ

โž› Day 15-17: INSERT, UPDATE, DELETE โ€“ altering your data.

โž› Day 18-20: Subqueries, nested queries, and derived tables.

โž› Day 21: Practice session โ€“ work on a mini-project.

๐—ช๐—ฒ๐—ฒ๐—ธ ๐Ÿฐ: ๐—”๐—ฑ๐˜ƒ๐—ฎ๐—ป๐—ฐ๐—ฒ๐—ฑ ๐—ฆ๐—ค๐—Ÿ ๐—ง๐—ผ๐—ฝ๐—ถ๐—ฐ๐˜€ ๐—ฎ๐—ป๐—ฑ ๐—ฃ๐—ฟ๐—ผ๐—ท๐—ฒ๐—ฐ๐˜

โž› Day 22-24: Window functions, RANK, DENSE_RANK, ROW_NUMBER.

โž› Day 25-27: Creating and managing indexes, views, and stored procedures.

โž› Day 28-30: Capstone project โ€“ work with real-world data to design and query a database.

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

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

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

Hope it helps :)
โค5๐Ÿ‘3
SQL Number Functions ๐Ÿ‘†
๐Ÿ‘7โค2
SQL Joins โœ…
๐Ÿ‘5โค4๐Ÿ‘1
SQL books wonโ€™t teach you this.

Natural Keys vs. Autoincrement IDs vs. Public IDs. (or maybe all together)


๐—ก๐—ฎ๐˜๐˜‚๐—ฟ๐—ฎ๐—น ๐—ž๐—ฒ๐˜†๐˜€

Natural keys carry intrinsic meaning because they are part of the domain.

They are directly related to the data, making them intuitive and easy to understand. Examples include email addresses or employee IDs.

The problem is that they are usually not good for performance, but they can also be a security risk if you expose them.


๐—”๐˜‚๐˜๐—ผ๐—ถ๐—ป๐—ฐ๐—ฟ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—œ๐——๐˜€

Autoincrement IDs automatically generate unique integers to identify rows within a table.

They are often used as primary keys.

Simple integers are fast for the database to index and query. They provide optimal performance.

However, they are vulnerable to enumeration attacks since predicting the next or previous record is easy.


๐—ฃ๐˜‚๐—ฏ๐—น๐—ถ๐—ฐ ๐—œ๐——๐˜€ (๐—จ๐—จ๐—œ๐——๐˜€)

UUIDs (Universally Unique Identifiers) are 128-bit identifiers used to uniquely identify information without relying on a centralized authority.

They are difficult to guess, making them suitable for public exposure in APIs.

The problem is they are larger and more complex than integers. This can impact performance, particularly in indexing and storage.


๐—™๐—ถ๐—ป๐—ฑ๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ฆ๐˜„๐—ฒ๐—ฒ๐˜ ๐—ฆ๐—ฝ๐—ผ๐˜: ๐—” ๐— ๐—ถ๐˜…๐—ฒ๐—ฑ ๐—”๐—ฝ๐—ฝ๐—ฟ๐—ผ๐—ฎ๐—ฐ๐—ต

Combining different types of keys can offer a balanced solution:

โ€ข InternalID: Used for internal operations and relationships between tables.

โ€ข PublicID: Used in API responses and endpoints to securely reference user records.

โ€ข Email (Natural Key): Used to ensure unique identification of users within the business logic.


The mixed approach keeps your system fast, secure, and easy to understand.

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

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

Hope it helps :)
๐Ÿ‘4โค2
SQL Query Logical Order
โค10๐Ÿ‘1
How a SQL query gets executed internally - Lets see step by step!

We all know SQL, but most of us do not understand the internals of it.

Let me take an example to explain this better.

Select p.plan_name, count(plan_id) as total_count
From plans p
Join subscriptions s on s.plan_id=p.plan_id
Where p.plan_name !=โ€™premiumโ€™
Group by p.plan_name
Having count(plan_id) > 100
Order by p.plan_name
Limit 10;

Step 01: Get the table data required to run the sql query
Operations: FROM, JOIN (From plans p, Join subscriptions s)

Step 02: Filter the data rows
Operations: WHERE (where p.plan_name=โ€™premiumโ€™)

Step 03: Group the data
Operations: GROUP (group by p.plan_name)

Step 04: Filter the grouped data
Operations: HAVING (having count(plan_id) > 100)

Step 05: Select the data columns
Operations: SELECT (select p.plan_name, count(p.plan_id)

Step 06: Order the data
Operations: ORDER BY (order by p.plan_name)

Step 07: Limit the data rows
Operations: LIMIT (limit 100)

Knowing the Internals really help.

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

Like this post if you need answer for the above question๐Ÿ‘โค๏ธ

Hope it helps :)
โค2๐Ÿ‘2
SQL for Data Analysis

Most people learn SQL just enough to pull some data. But if you really understand it, you can analyze massive datasets without touching Excel or Python.

Here are 8 game-changing SQL concepts that will make you a data pro:

๐Ÿ‘‡


1. Stop pulling raw data. Start pulling insights.

The biggest mistake? Running a query that gives you everything and then filtering it later.

Good analysts donโ€™t pull raw data. They shape the data before it even reaches them.

2. โ€œSELECT โ€ is a rookie move.

Pulling all columns is lazy and slow.

A pro only selects what they need.
โœ”๏ธ Fewer columns = Faster queries
โœ”๏ธ Less noise = Clearer insights

The more precise your query, the less time you waste cleaning data.

3. GROUP BY is your best friend.

You donโ€™t need 100,000 rows of transactions. What you need is:
โœ”๏ธ Sales per region
โœ”๏ธ Average order size per customer
โœ”๏ธ Number of signups per month

Grouping turns chaotic data into useful summaries.

4. Joins = Connecting the dots.

Your most important data is split across multiple tables.

Want to know how much each customer spent? You need to join:
โœ”๏ธ Customer info
โœ”๏ธ Order history
โœ”๏ธ Payments

Joins = unlocking hidden insights.

5. Window functions will blow your mind.

They let you:
โœ”๏ธ Rank customers by total purchases
โœ”๏ธ Calculate rolling averages
โœ”๏ธ Compare each row to the overall trend

Itโ€™s like pivot tables, but way more powerful.

6. CTEs will save you from spaghetti SQL.

Instead of writing a 50-line nested query, break it into steps.

CTEs (Common Table Expressions) make your SQL:
โœ”๏ธ Easier to read
โœ”๏ธ Easier to debug
โœ”๏ธ Reusable

Good SQL is clean SQL.

7. Indexes = Speed.

If your queries take forever, your database is probably doing unnecessary work.

Indexes help databases find data faster.

If you work with large datasets, this is a game changer.

SQL isnโ€™t just about pulling data. Itโ€™s about analyzing, transforming, and optimizing it.

Master these 8 concepts, and youโ€™ll never look at SQL the same way again.

Join us on WhatsApp: https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v
โค3๐Ÿ‘3
Top Tools to learn Data Engineering

Join for more data engineering resources: https://t.iss.one/sql_engineer
โค2๐Ÿ‘2
๐Ÿ˜‚๐Ÿ˜‚
๐Ÿคฃ20๐Ÿค”3
The way you WRITE an SQL query differs from the way SQL PROCESSES it DataAnalytics

Your query might look like this:

1. SELECT
2. FROM
3. WHERE
4. GROUP BY
5. HAVING
6. ORDER BY

But SQL executes queries in a different order:

FROM (identifies table)
WHERE (filters rows)
GROUP BY (groups rows)
HAVING (filters grouped rows)
SELECT (chooses columns to display)
ORDER BY (sorts final output)
โค14๐Ÿ‘8๐ŸŽ‰1
Why SQL is a Must-Have Skill?

If you're working with data, mastering SQL is non-negotiable! Itโ€™s the backbone of handling and making sense of vast datasets in any industry.

โ—† Data at Your Fingertips
Effortlessly organize, retrieve, and manage large datasets to make informed decisions faster.

โ—† Stay Organized
Use primary and foreign keys to keep your data accurate and connected across tables.

โ—† Unlock Insights
Combine data from multiple sources and uncover trends using SQL's powerful query capabilities.

โ—† Efficiency Matters
Optimize your databases with normalization and avoid unnecessary redundancy.

โ—† Advanced Tools
From ACID transactions to optimizing with DELETE vs TRUNCATE, SQL makes sure your data is consistent and secure.

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

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

Hope it helps :)
๐Ÿ‘4โค1
SQL Zero to Hero ๐Ÿ’ช
โค5๐Ÿ‘1
SQL From Basic to Advanced level

Basic SQL is ONLY 7 commands:
- SELECT
- FROM
- WHERE (also use SQL comparison operators such as =, <=, >=, <> etc.)
- ORDER BY
- Aggregate functions such as SUM, AVERAGE, COUNT etc.
- GROUP BY
- CREATE, INSERT, DELETE, etc.
You can do all this in just one morning.

Once you know these, take the next step and learn commands like:
- LEFT JOIN
- INNER JOIN
- LIKE
- IN
- CASE WHEN
- HAVING (undertstand how it's different from GROUP BY)
- UNION ALL
This should take another day.

Once both basic and intermediate are done, start learning more advanced SQL concepts such as:
- Subqueries (when to use subqueries vs CTE?)
- CTEs (WITH AS)
- Stored Procedures
- Triggers
- Window functions (LEAD, LAG, PARTITION BY, RANK, DENSE RANK)
These can be done in a couple of days.
Learning these concepts is NOT hard at all

- what takes time is practice and knowing what command to use when. How do you master that?
- First, create a basic SQL project
- Then, work on an intermediate SQL project (search online) -

Lastly, create something advanced on SQL with many CTEs, subqueries, stored procedures and triggers etc.

This is ALL you need to become a badass in SQL, and trust me when I say this, it is not rocket science. It's just logic.

Remember that practice is the key here. It will be more clear and perfect with the continous practice

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

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

Hope it helps :)
๐Ÿ‘8โค7๐Ÿ˜1
5 frequently Asked SQL Interview Questions with Answers in Data Engineering interviews:
๐ƒ๐ข๐Ÿ๐Ÿ๐ข๐œ๐ฎ๐ฅ๐ญ๐ฒ - ๐Œ๐ž๐๐ข๐ฎ๐ฆ

โšซ๏ธDetermine the Top 5 Products with the Highest Revenue in Each Category.
Schema: Products (ProductID, Name, CategoryID), Sales (SaleID, ProductID, Amount)

WITH ProductRevenue AS (
SELECT p.ProductID,
p.Name,
p.CategoryID,
SUM(s.Amount) AS TotalRevenue,
RANK() OVER (PARTITION BY p.CategoryID ORDER BY SUM(s.Amount) DESC) AS RevenueRank
FROM Products p
JOIN Sales s ON p.ProductID = s.ProductID
GROUP BY p.ProductID, p.Name, p.CategoryID
)
SELECT ProductID, Name, CategoryID, TotalRevenue
FROM ProductRevenue
WHERE RevenueRank <= 5;

โšซ๏ธ Identify Employees with Increasing Sales for Four Consecutive Quarters.
Schema: Sales (EmployeeID, SaleDate, Amount)

WITH QuarterlySales AS (
SELECT EmployeeID,
DATE_TRUNC('quarter', SaleDate) AS Quarter,
SUM(Amount) AS QuarterlyAmount
FROM Sales
GROUP BY EmployeeID, DATE_TRUNC('quarter', SaleDate)
),
SalesTrend AS (
SELECT EmployeeID,
Quarter,
QuarterlyAmount,
LAG(QuarterlyAmount, 1) OVER (PARTITION BY EmployeeID ORDER BY Quarter) AS PrevQuarter1,
LAG(QuarterlyAmount, 2) OVER (PARTITION BY EmployeeID ORDER BY Quarter) AS PrevQuarter2,
LAG(QuarterlyAmount, 3) OVER (PARTITION BY EmployeeID ORDER BY Quarter) AS PrevQuarter3
FROM QuarterlySales
)
SELECT EmployeeID, Quarter, QuarterlyAmount
FROM SalesTrend
WHERE QuarterlyAmount > PrevQuarter1 AND PrevQuarter1 > PrevQuarter2 AND PrevQuarter2 > PrevQuarter3;

โšซ๏ธ List Customers Who Made Purchases in Each of the Last Three Years.
Schema: Orders (OrderID, CustomerID, OrderDate)

WITH YearlyOrders AS (
SELECT CustomerID,
EXTRACT(YEAR FROM OrderDate) AS OrderYear
FROM Orders
GROUP BY CustomerID, EXTRACT(YEAR FROM OrderDate)
),
RecentYears AS (
SELECT DISTINCT OrderYear
FROM Orders
WHERE OrderDate >= CURRENT_DATE - INTERVAL '3 years'
),
CustomerYearlyOrders AS (
SELECT CustomerID,
COUNT(DISTINCT OrderYear) AS YearCount
FROM YearlyOrders
WHERE OrderYear IN (SELECT OrderYear FROM RecentYears)
GROUP BY CustomerID
)
SELECT CustomerID
FROM CustomerYearlyOrders
WHERE YearCount = 3;


โšซ๏ธ Find the Third Lowest Price for Each Product Category.
Schema: Products (ProductID, Name, CategoryID, Price)

WITH RankedPrices AS (
SELECT CategoryID,
Price,
DENSE_RANK() OVER (PARTITION BY CategoryID ORDER BY Price ASC) AS PriceRank
FROM Products
)
SELECT CategoryID, Price
FROM RankedPrices
WHERE PriceRank = 3;

โšซ๏ธ Identify Products with Total Sales Exceeding a Specified Threshold Over the Last 30 Days.
Schema: Sales (SaleID, ProductID, SaleDate, Amount)

WITH RecentSales AS (
SELECT ProductID,
SUM(Amount) AS TotalSales
FROM Sales
WHERE SaleDate >= CURRENT_DATE - INTERVAL '30 days'
GROUP BY ProductID
)
SELECT ProductID, TotalSales
FROM RecentSales
WHERE TotalSales > 200;

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

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

Hope it helps :)
๐Ÿ‘9โค1
Types Of Databases
๐Ÿ‘5๐Ÿ‘1