SQL Fundamentals
1.1 Introduction to Databases and SQL
1.1.1 What is SQL?
Remember the last time you ordered something online? That smooth experience where your past orders, shipping details, and payment info just magically appeared? Behind that seamless interaction was SQL, quietly doing its thing.
SQL stands for Structured Query Language, and it's been around since the 1970s. That's right – it's older than the internet, older than Windows, and probably older than many of the developers using it today. And unlike that ancient flip phone in your drawer, SQL has aged like fine wine.
It's the reason why:
Netflix remembers where you left off in that show you're binging
Your bank doesn't accidentally give your money to someone else
Amazon knows exactly what's in stock and where it's sitting
1.1.2 Types of Databases
A. Relational Databases (SQL)/RDBMS
These are like extremely organized spreadsheets on steroids. Data is stored in tables with rows and columns, where each piece of information has a clear relationship with other data. SQL is used for interacting with RDBMS. SQL code used on one RDBMS is not always portable to another without modification.
Popular examples include:
MySQL: The friendly neighborhood database, perfect for small to medium projects
PostgreSQL: The Swiss Army knife of databases, known for its extensibility
Oracle: The corporate heavyweight, powering Fortune 500 companies
SQL Server: Microsoft's take on databases, deeply integrated with Windows
B. Non-Relational Databases (NoSQL)/NRDBMS
These databases break free from the traditional table structure. There’s no set language standard. Most NRDBMS will implement their own language for performing C.R.U.D and administrative operations on the databases.
Examples include:
· MongoDB: Stores data in flexible, JSON-like documents
· Redis: Lightning-fast data storage in memory
· Cassandra: Handles massive amounts of data across multiple servers.
1.2: Basic SQL Queries
1.2.1 SELECT Statement
The SELECT statement is like a data detective – it helps you find and retrieve exactly what you're looking for in your database. Think of it as the "show me" command of SQL.
Basic Syntax:
Common SELECT Patterns:
-- Get all columns from a table
-- Get specific columns
-- Get unique values
1.2.2 WHERE clause
The WHERE clause is your database's bouncer – it helps you filter out data that doesn't meet your criteria. It's the difference between "show me all employees" and "show me employees who make over $50,000."
Basic Syntax:
Common WHERE Conditions:
-- Equal to
-- Greater than
-- Multiple conditions using AND/OR
-- Pattern matching using LIKE
1.2.3 ORDER BY and GROUP BY clause
ORDER BY: Sorting Your Data
Think of ORDER BY as your data organizer. It's like sorting books by title, author, or publication date.
Basic Syntax:
Examples:
-- Sort employees by salary (highest first)
-- Sort by multiple columns
GROUP BY: Summarizing Your Data
GROUP BY helps you cluster similar data together, perfect for creating summaries and reports.
Basic Syntax:
Examples:
-- Count employees in each department
-- Calculate average salary by department
1.2.4 Basic data filtering
Beyond simple WHERE clauses, SQL offers several ways to filter your data more precisely:
1. Comparison Operators:
-- Between a range
-- In a list
-- Is NULL or Not NULL
2. Text Filtering:
-- Starts with
-- Ends with
-- Contains
Pro Tips:
Always start with simple queries and build up complexity gradually
Use table aliases for cleaner, more readable queries
Remember to test your WHERE conditions carefully
Use LIMIT when testing queries on large datasets
1.3: Data Manipulation Language (DML)
SQL is actually a hybrid language and has four types of languages and in one of them is Data Manipulation Language (DML) which is used for inserting, updating and deleting data from the database.
1.3.1 INSERT operations
used to add new records to a table.
Syntax:
Use Cases: INSERT operations are commonly used, such as adding new user accounts or new entries in a product catalog.
1.3.2 UPDATE operations
used to modify existing data in a table.
Syntax:
Example:
Use Cases: Highlight situations where updates are necessary, like changing a user's email address or updating product prices.
1.3.3 DELETE operations
used to remove records from a table.
Syntax:
Example:
Use Cases: deletions are needed when removing outdated records or deleting user accounts.
1.3.4 C.R.U.D operations
1. Create
Corresponds to: INSERT operation.
Purpose: Adding new records to the database.
2. Read
Corresponds to: SELECT operation.
Purpose: Retrieving data from the database.
Syntax:
3. Update
Corresponds to: UPDATE operation.
Purpose: Modifying existing records in the database.
4. Delete
Corresponds to: DELETE operation.
Purpose: Removing records from the database.
Data Engineer@Alternative Path |SQL | Python | Snowflake | Cloud | Spark | Airflow | Data visualization | ETL | Azure | Azure synapse analytics
4moThis is so helpful !! Loving this !