1. Home
  2. Coding Hub
  3. Coding Notes
  4. Sql
  5. Introduction
45% OFFBLACK FRIDAY SALE
Sale ends in
02Days
18Hrs
14Mins
34Secs
Claim
  1. Home
  2. Coding Hub
  3. Coding Notes
  4. Sql
  5. Introduction

Limited time offer

45% OFF

BLACK FRIDAY SALE

Sale ends in

02Days
18Hrs
14Mins
34Secs
Claim
HTMLCSSJavaScriptPythonJavaC++React.jsSQLNext.jsNode.js
Underrated CoderUnderrated Coder
Underrated CoderUnderrated Coder
Resources
  • Coding Hub
  • Roadmap
  • DSA Sheet
  • Coding Notes
  • Coding Quiz
  • Code Compiler
  • Coder AI
Interview Preparation
  • 1:1 Mentorship
  • Interview Q&A
Community
  • Blog
  • Coder Community
Help & Support
  • Contact Us
  • Troubleshooting
  • Report a Bug
HTMLCSSJavaScriptPythonJavaC++React.jsSQLNext.jsNode.js
Outline
SQL IntroductionRDBMS ArchitectureData Types (VARCHAR)CREATE TABLETable ConstraintsALTER & DROP
SELECT BasicsWHERE ClauseComparison OperatorsLogical OperatorsLIKE Pattern MatchingORDER BY & LIMIT
Aggregate FunctionsGROUP BY ClauseHAVING vs WHEREDISTINCT DeduplicationHandling NULL Values
Relational Joins IntroINNER JOINLEFT & RIGHT JOINFULL OUTER JOINCROSS JOINSelf JoinsUNION vs UNION ALL
Scalar SubqueriesCorrelated SubqueriesIN vs EXISTSAnti-Joins (NOT EXISTS)CASE WHEN ExpressionsString & Date Functions
Common Table ExpressionsRecursive CTEsWindow Functions (OVER)PARTITION BY MechanicsROW_NUMBER() RankingRANK() vs DENSE_RANK()LEAD() & LAG()Running Totals (ROWS)
INSERT StatementsUPDATE OperationsDELETE vs TRUNCATEUPSERT & MERGEACID Transactions
Primary vs Foreign KeysNormalization (1NF–3NF)Denormalization Trade-offsViews vs Materialized ViewsSQL Injection Prevention
Clustered vs Non-ClusteredComposite IndexesEXPLAIN Execution PlansSolving N+1 Queries
SQL

SQL Introduction

Overview

Structured Query Language (SQL) is the standard language for communicating with Relational Databases (like MySQL, PostgreSQL, and Oracle). Unlike general-purpose programming languages (Python, Java) which are 'Imperative' (you write exactly how to do a task step-by-step), SQL is 'Declarative' (you describe what data you want, and the database engine figures out the fastest way to retrieve it). Mastering SQL is non-negotiable for backend engineers, data scientists, and anyone building robust applications.

Syntax

sql
-- 1. DQL (Data Query Language): Retrieving data
SELECT first_name, email FROM users;

-- 2. DDL (Data Definition Language): Creating structure
CREATE TABLE users (id INT, name VARCHAR(50));

-- 3. DML (Data Manipulation Language): Modifying data
INSERT INTO users (id, name) VALUES (1, 'Alice');
UPDATE users SET name = 'Alicia' WHERE id = 1;

-- 4. DCL (Data Control Language): Security & Permissions
GRANT SELECT ON users TO analyst_role;

Common Pitfalls

  • Confusing SQL with a Database Engine. SQL is just the written language. PostgreSQL, MySQL, and SQLite are the actual software engines that read the SQL and execute it. While SQL is a standard (ANSI SQL), every engine has its own slight variations ('dialects').
  • Treating SQL like a loop in Python. Beginners often try to query data row-by-row and process it in a loop. SQL is entirely built on 'Set Theory'. You should always think about manipulating entire blocks of data (Sets) simultaneously in a single query.

Interview Questions

What is the fundamental difference between DDL and DML in SQL?

DDL (Data Definition Language) commands like CREATE, ALTER, and DROP modify the actual structural schema of the database tables. DML (Data Manipulation Language) commands like INSERT, UPDATE, and DELETE modify the actual row data living inside those tables.

Real-World Example

How a Node.js backend executes a declarative SQL query to log in a user.

example
sql
// The backend just sends the SQL string to the database engine.
// The engine parses it, optimizes it, fetches the data from disk, and returns the rows.
const username = 'johndoe';
const query = "SELECT id, password_hash FROM users WHERE username = $1";
const result = await db.execute(query, [username]);

Check Your Knowledge

Test your understanding of SQL Introduction with these quick questions.

Question 1

Because SQL allows you to specify the result you want without writing the step-by-step loops to get it, it is classified as what kind of language?

Take SQL Quiz
Outline
SQL IntroductionRDBMS ArchitectureData Types (VARCHAR)CREATE TABLETable ConstraintsALTER & DROP
SELECT BasicsWHERE ClauseComparison OperatorsLogical OperatorsLIKE Pattern MatchingORDER BY & LIMIT
Aggregate FunctionsGROUP BY ClauseHAVING vs WHEREDISTINCT DeduplicationHandling NULL Values
Relational Joins IntroINNER JOINLEFT & RIGHT JOINFULL OUTER JOINCROSS JOINSelf JoinsUNION vs UNION ALL
Scalar SubqueriesCorrelated SubqueriesIN vs EXISTSAnti-Joins (NOT EXISTS)CASE WHEN ExpressionsString & Date Functions
Common Table ExpressionsRecursive CTEsWindow Functions (OVER)PARTITION BY MechanicsROW_NUMBER() RankingRANK() vs DENSE_RANK()LEAD() & LAG()Running Totals (ROWS)
INSERT StatementsUPDATE OperationsDELETE vs TRUNCATEUPSERT & MERGEACID Transactions
Primary vs Foreign KeysNormalization (1NF–3NF)Denormalization Trade-offsViews vs Materialized ViewsSQL Injection Prevention
Clustered vs Non-ClusteredComposite IndexesEXPLAIN Execution PlansSolving N+1 Queries
SQL

SQL Introduction

Overview

Structured Query Language (SQL) is the standard language for communicating with Relational Databases (like MySQL, PostgreSQL, and Oracle). Unlike general-purpose programming languages (Python, Java) which are 'Imperative' (you write exactly how to do a task step-by-step), SQL is 'Declarative' (you describe what data you want, and the database engine figures out the fastest way to retrieve it). Mastering SQL is non-negotiable for backend engineers, data scientists, and anyone building robust applications.

Syntax

sql
-- 1. DQL (Data Query Language): Retrieving data
SELECT first_name, email FROM users;

-- 2. DDL (Data Definition Language): Creating structure
CREATE TABLE users (id INT, name VARCHAR(50));

-- 3. DML (Data Manipulation Language): Modifying data
INSERT INTO users (id, name) VALUES (1, 'Alice');
UPDATE users SET name = 'Alicia' WHERE id = 1;

-- 4. DCL (Data Control Language): Security & Permissions
GRANT SELECT ON users TO analyst_role;

Common Pitfalls

  • Confusing SQL with a Database Engine. SQL is just the written language. PostgreSQL, MySQL, and SQLite are the actual software engines that read the SQL and execute it. While SQL is a standard (ANSI SQL), every engine has its own slight variations ('dialects').
  • Treating SQL like a loop in Python. Beginners often try to query data row-by-row and process it in a loop. SQL is entirely built on 'Set Theory'. You should always think about manipulating entire blocks of data (Sets) simultaneously in a single query.

Interview Questions

What is the fundamental difference between DDL and DML in SQL?

DDL (Data Definition Language) commands like CREATE, ALTER, and DROP modify the actual structural schema of the database tables. DML (Data Manipulation Language) commands like INSERT, UPDATE, and DELETE modify the actual row data living inside those tables.

Real-World Example

How a Node.js backend executes a declarative SQL query to log in a user.

example
sql
// The backend just sends the SQL string to the database engine.
// The engine parses it, optimizes it, fetches the data from disk, and returns the rows.
const username = 'johndoe';
const query = "SELECT id, password_hash FROM users WHERE username = $1";
const result = await db.execute(query, [username]);

Check Your Knowledge

Test your understanding of SQL Introduction with these quick questions.

Question 1

Because SQL allows you to specify the result you want without writing the step-by-step loops to get it, it is classified as what kind of language?

Take SQL Quiz

SQL coding notes for Indian students — 52 topics with syntax, pitfalls, interview Q&A, and inline quizzes.

Topics in SQL

  1. SQL Introduction
  2. RDBMS Architecture
  3. Data Types (VARCHAR)
  4. CREATE TABLE
  5. Table Constraints
  6. ALTER & DROP
  7. SELECT Basics
  8. WHERE Clause
  9. Comparison Operators
  10. Logical Operators
  11. LIKE Pattern Matching
  12. ORDER BY & LIMIT
  13. Aggregate Functions
  14. GROUP BY Clause
  15. HAVING vs WHERE
  16. DISTINCT Deduplication
  17. Handling NULL Values
  18. Relational Joins Intro
  19. INNER JOIN
  20. LEFT & RIGHT JOIN
  21. FULL OUTER JOIN
  22. CROSS JOIN
  23. Self Joins
  24. UNION vs UNION ALL
  25. Scalar Subqueries
  26. Correlated Subqueries
  27. IN vs EXISTS
  28. Anti-Joins (NOT EXISTS)
  29. CASE WHEN Expressions
  30. String & Date Functions
  31. Common Table Expressions
  32. Recursive CTEs
  33. Window Functions (OVER)
  34. PARTITION BY Mechanics
  35. ROW_NUMBER() Ranking
  36. RANK() vs DENSE_RANK()
  37. LEAD() & LAG()
  38. Running Totals (ROWS)
  39. INSERT Statements
  40. UPDATE Operations
  41. DELETE vs TRUNCATE
  42. UPSERT & MERGE
  43. ACID Transactions
  44. Primary vs Foreign Keys
  45. Normalization (1NF–3NF)
  46. Denormalization Trade-offs
  47. Views vs Materialized Views
  48. SQL Injection Prevention
  49. Clustered vs Non-Clustered
  50. Composite Indexes
  51. EXPLAIN Execution Plans
  52. Solving N+1 Queries

Study next

  • All coding notes
  • SQL notes home
  • SQL certification quiz
  • Online compiler
  • Interview Q&A

SQL coding notes for Indian students — 52 topics with syntax, pitfalls, interview Q&A, and inline quizzes.

Topics in SQL

  1. SQL Introduction
  2. RDBMS Architecture
  3. Data Types (VARCHAR)
  4. CREATE TABLE
  5. Table Constraints
  6. ALTER & DROP
  7. SELECT Basics
  8. WHERE Clause
  9. Comparison Operators
  10. Logical Operators
  11. LIKE Pattern Matching
  12. ORDER BY & LIMIT
  13. Aggregate Functions
  14. GROUP BY Clause
  15. HAVING vs WHERE
  16. DISTINCT Deduplication
  17. Handling NULL Values
  18. Relational Joins Intro
  19. INNER JOIN
  20. LEFT & RIGHT JOIN
  21. FULL OUTER JOIN
  22. CROSS JOIN
  23. Self Joins
  24. UNION vs UNION ALL
  25. Scalar Subqueries
  26. Correlated Subqueries
  27. IN vs EXISTS
  28. Anti-Joins (NOT EXISTS)
  29. CASE WHEN Expressions
  30. String & Date Functions
  31. Common Table Expressions
  32. Recursive CTEs
  33. Window Functions (OVER)
  34. PARTITION BY Mechanics
  35. ROW_NUMBER() Ranking
  36. RANK() vs DENSE_RANK()
  37. LEAD() & LAG()
  38. Running Totals (ROWS)
  39. INSERT Statements
  40. UPDATE Operations
  41. DELETE vs TRUNCATE
  42. UPSERT & MERGE
  43. ACID Transactions
  44. Primary vs Foreign Keys
  45. Normalization (1NF–3NF)
  46. Denormalization Trade-offs
  47. Views vs Materialized Views
  48. SQL Injection Prevention
  49. Clustered vs Non-Clustered
  50. Composite Indexes
  51. EXPLAIN Execution Plans
  52. Solving N+1 Queries

Study next

  • All coding notes
  • SQL notes home
  • SQL certification quiz
  • Online compiler
  • Interview Q&A