SQL Basics and Intermediate
Chapter 1
To install DB Browser for SQLite using Homebrew, follow these steps:
Installation Steps
Update Homebrew (optional but recommended):
brew updateInstall DB Browser for SQLite:
brew install --cask db-browser-for-sqliteVerify Installation: Once installed, you can open the app from your Applications folder or by typing:
open -a "DB Browser for SQLite"
About DB Browser for SQLite
DB Browser for SQLite is a visual tool to help you interact with SQLite databases. It allows you to:
Create, edit, and browse SQLite databases.
Run SQL queries in a user-friendly interface.
Export and import data easily.
Let me know if you encounter any issues!
Chapter 2
Here’s how you can perform basic CRUD operations in SQLite directly from the terminal:
Step 1: Open SQLite
Run the SQLite command-line interface:
This creates or opens the database file example.db.
Step 2: Create a Table
Execute the following SQL command to create a table:
Step 3: Insert Data (CREATE)
Insert a record into the table:
Step 4: Read Data (READ)
Retrieve all data from the table:
Output:
Step 5: Update Data (UPDATE)
Modify a record in the table:
Verify the change:
Step 6: Delete Data (DELETE)
Remove a record from the table:
Verify the deletion:
Step 7: Exit SQLite
Type the following to exit the SQLite shell:
Summary of Commands
Start SQLite:
sqlite3 example.dbCreate Table:
Insert Data:
Read Data:
Update Data:
Delete Data:
Exit:
.exit
You’ve now performed CRUD operations in SQLite directly from the terminal!
Chapter 3
To install DB Browser for SQLite using Homebrew, follow these steps:
Installation Steps
Update Homebrew (optional but recommended):
Install DB Browser for SQLite:
Verify Installation: Once installed, you can open the app from your Applications folder or by typing:
About DB Browser for SQLite
DB Browser for SQLite is a visual tool to help you interact with SQLite databases. It allows you to:
Create, edit, and browse SQLite databases.
Run SQL queries in a user-friendly interface.
Export and import data easily.
Chapter 4
Here are 20 SQL commands with slightly more complex operations that you can try in SQLite:
1. Add a New Column to an Existing Table
2. Insert Multiple Rows
3. Filter Rows with WHERE and AND
WHERE and AND4. Sort Results with ORDER BY
ORDER BY5. Limit Results with LIMIT
LIMIT6. Find Rows with Missing Data (NULL)
NULL)7. Update Multiple Columns
8. Delete Rows Matching a Condition
9. Rename a Table
10. Group Data with GROUP BY
GROUP BY11. Filter Groups with HAVING
HAVING12. Calculate Aggregate Functions
13. Use Subqueries
14. Join Two Tables (INNER JOIN)
INNER JOIN)15. Left Join to Include Unmatched Rows
16. Count Rows
17. Find Unique Values with DISTINCT
DISTINCT18. Create a View
Query the view:
19. Use a CASE Statement
CASE Statement20. Drop a Table
These commands are a mix of data manipulation, querying, and database structure management, providing you with a deeper understanding of SQL operations.
Chapter 5
Here are 20 more advanced SQL commands that showcase complex queries and operations in SQLite. These commands include advanced joins, subqueries, window functions, and more:
1. Self Join
2. Find Duplicate Records
3. Recursive Query for Hierarchical Data
4. Correlated Subquery
5. Window Function for Ranking
6. Cumulative Sum with Window Function
7. Find Users with Maximum Orders
8. Delete Duplicate Rows (Keep One Record)
9. Find the Second Highest Age
10. Find Users with No Orders
11. Update Using a Subquery
12. Pivot Table Using Conditional Aggregation
13. Join Three Tables
14. Find the Most Common Age
15. Aggregate with Multiple Grouping Levels
16. Insert Data from Another Table
17. Find Users Who Made Orders Above a Certain Total
18. Full Outer Join Simulation
19. JSON Data Query
20. Create a Trigger
These commands provide a range of advanced SQL techniques, including recursive queries, window functions, triggers, and JSON operations. They are ideal for deepening your knowledge of SQL and mastering complex use cases.
Chapter 6
Here are 20 more complex SQL commands to expand your knowledge further. These cover advanced techniques like CTEs, indexing, transactions, and more:
1. Find the Top 3 Ages
2. Conditional Aggregation
3. Find All Users with the Same Age as Others
4. Row Number Using Window Function
5. Dense Rank Using Window Function
6. NTILE Using Window Function
7. Common Table Expression (CTE) for Summing Ages
8. Generate Sequential Numbers
9. Composite Primary Key
10. Create an Index
11. Using Transactions
12. Find Users Without Orders Using EXISTS
EXISTS13. Calculate Percentage
14. Dynamic Group By with CTE
15. Find Gaps in a Sequence
16. Create a Stored Procedure Equivalent with Triggers
17. Update Using Joins
18. Generate Running Totals
19. Delete Rows with Limit
SQLite doesn’t support DELETE ... LIMIT, but you can emulate it:
20. Check Query Execution Plan
These commands showcase advanced SQL concepts like window functions, recursive queries, indexing, transactions, and triggers. They are ideal for diving deeper into SQL's capabilities! Let me know if you need clarification on any of them.
Chapter 7
Here are 20 more advanced and complex SQL queries covering scenarios like advanced joins, analytical functions, hierarchical data, and dynamic queries.
1. Find Consecutive Records
2. Rank Users by Age and Handle Ties
3. Calculate Rolling Average
4. Generate Sequential Dates
5. Find the Longest Consecutive Sequence
6. Identify Overlapping Date Ranges
7. Combine Two Columns into One
8. Aggregate JSON Data
9. Find the Nth Highest Value
10. Check if Two Tables Have the Same Data
11. Dynamic Pivot Table
12. Find Rows with Greatest N Per Group
13. Split Strings into Rows
14. Recursive Fibonacci Sequence
15. Calculate Percentile
16. Union with Deduplication
17. Detect Data Drift
18. Simulate Full Outer Join
19. Cohort Analysis
20. Find Users With Matching Initials
These examples cover a variety of advanced SQL use cases, including recursive queries, dynamic pivots, advanced joins, and analytical functions.
Chapter 8
Here are 20 more complex SQL queries that dive even deeper into advanced SQL concepts, such as data manipulation, window functions, advanced subqueries, and dynamic operations.
1. Find Users Who Placed Orders on Every Day of a Month
2. Calculate Median Age
3. Cumulative Percentage
4. Find the Largest Gap in Ages
5. List All Pairs of Users With the Same Age
6. Find Users with More Than One Email
7. Find the Maximum Order Amount for Each User
8. Filter Outliers Using the Interquartile Range
9. Rank Users by Total Order Amount
10. Find Consecutive Days with Orders
11. Find Users Who Have Not Ordered for the Past 30 Days
12. Find Top Spending Users by Month
13. Find Users Who Share the Same First Name
14. Check for Referential Integrity Violations
15. Update a Column Based on a Condition
16. Find Orders Where the Total Amount Exceeds the User’s Average
17. Generate All Possible Pairs of Users
18. Find Users Who Made More Than One Order in a Day
19. Calculate Time Difference Between Orders
20. Dynamic Query with Aggregates
These advanced SQL queries cover a wide range of scenarios and techniques, such as handling time-based data, advanced grouping, filtering outliers, and generating dynamic aggregations.
Last updated