SQL Basics and Intermediate
Chapter 1
To install DB Browser for SQLite using Homebrew, follow these steps:
Installation Steps
Update Homebrew (optional but recommended):
Copy
brew updateInstall DB Browser for SQLite:
Copy
brew install --cask db-browser-for-sqliteVerify Installation: Once installed, you can open the app from your Applications folder or by typing:
Copy
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:
Copy
This creates or opens the database file example.db.
Step 2: Create a Table
Execute the following SQL command to create a table:
Copy
Step 3: Insert Data (CREATE)
Insert a record into the table:
Copy
Step 4: Read Data (READ)
Retrieve all data from the table:
Copy
Output:
Copy
Step 5: Update Data (UPDATE)
Modify a record in the table:
Copy
Verify the change:
Copy
Step 6: Delete Data (DELETE)
Remove a record from the table:
Copy
Verify the deletion:
Copy
Step 7: Exit SQLite
Type the following to exit the SQLite shell:
Copy
Summary of Commands
Start SQLite:
sqlite3 example.dbCreate Table:
Copy
Insert Data:
Copy
Read Data:
Copy
Update Data:
Copy
Delete Data:
Copy
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):
Copy
Install DB Browser for SQLite:
Copy
Verify Installation: Once installed, you can open the app from your Applications folder or by typing:
Copy
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
Copy
2. Insert Multiple Rows
Copy
3. Filter Rows with WHERE and AND
Copy
4. Sort Results with ORDER BY
Copy
5. Limit Results with LIMIT
Copy
6. Find Rows with Missing Data (NULL)
Copy
7. Update Multiple Columns
Copy
8. Delete Rows Matching a Condition
Copy
9. Rename a Table
Copy
10. Group Data with GROUP BY
Copy
11. Filter Groups with HAVING
Copy
12. Calculate Aggregate Functions
Copy
13. Use Subqueries
Copy
14. Join Two Tables (INNER JOIN)
Copy
15. Left Join to Include Unmatched Rows
Copy
16. Count Rows
Copy
17. Find Unique Values with DISTINCT
Copy
18. Create a View
Copy
Query the view:
Copy
19. Use a CASE Statement
Copy
20. Drop a Table
Copy
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
Copy
2. Find Duplicate Records
Copy
3. Recursive Query for Hierarchical Data
Copy
4. Correlated Subquery
Copy
5. Window Function for Ranking
Copy
6. Cumulative Sum with Window Function
Copy
7. Find Users with Maximum Orders
Copy
8. Delete Duplicate Rows (Keep One Record)
Copy
9. Find the Second Highest Age
Copy
10. Find Users with No Orders
Copy
11. Update Using a Subquery
Copy
12. Pivot Table Using Conditional Aggregation
Copy
13. Join Three Tables
Copy
14. Find the Most Common Age
Copy
15. Aggregate with Multiple Grouping Levels
Copy
16. Insert Data from Another Table
Copy
17. Find Users Who Made Orders Above a Certain Total
Copy
18. Full Outer Join Simulation
Copy
19. JSON Data Query
Copy
20. Create a Trigger
Copy
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
Copy
2. Conditional Aggregation
Copy
3. Find All Users with the Same Age as Others
Copy
4. Row Number Using Window Function
Copy
5. Dense Rank Using Window Function
Copy
6. NTILE Using Window Function
Copy
7. Common Table Expression (CTE) for Summing Ages
Copy
8. Generate Sequential Numbers
Copy
9. Composite Primary Key
Copy
10. Create an Index
Copy
11. Using Transactions
Copy
12. Find Users Without Orders Using EXISTS
Copy
13. Calculate Percentage
Copy
14. Dynamic Group By with CTE
Copy
15. Find Gaps in a Sequence
Copy
16. Create a Stored Procedure Equivalent with Triggers
Copy
17. Update Using Joins
Copy
18. Generate Running Totals
Copy
19. Delete Rows with Limit
SQLite doesn’t support DELETE ... LIMIT, but you can emulate it:
Copy
20. Check Query Execution Plan
Copy
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
Copy
2. Rank Users by Age and Handle Ties
Copy
3. Calculate Rolling Average
Copy
4. Generate Sequential Dates
Copy
5. Find the Longest Consecutive Sequence
Copy
6. Identify Overlapping Date Ranges
Copy
7. Combine Two Columns into One
Copy
8. Aggregate JSON Data
Copy
9. Find the Nth Highest Value
Copy
10. Check if Two Tables Have the Same Data
Copy
11. Dynamic Pivot Table
Copy
12. Find Rows with Greatest N Per Group
Copy
13. Split Strings into Rows
Copy
14. Recursive Fibonacci Sequence
Copy
15. Calculate Percentile
Copy
16. Union with Deduplication
Copy
17. Detect Data Drift
Copy
18. Simulate Full Outer Join
Copy
19. Cohort Analysis
Copy
20. Find Users With Matching Initials
Copy
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
Copy
2. Calculate Median Age
Copy
3. Cumulative Percentage
Copy
4. Find the Largest Gap in Ages
Copy
5. List All Pairs of Users With the Same Age
Copy
6. Find Users with More Than One Email
Copy
7. Find the Maximum Order Amount for Each User
Copy
8. Filter Outliers Using the Interquartile Range
Copy
9. Rank Users by Total Order Amount
Copy
10. Find Consecutive Days with Orders
Copy
11. Find Users Who Have Not Ordered for the Past 30 Days
Copy
12. Find Top Spending Users by Month
Copy
13. Find Users Who Share the Same First Name
Copy
14. Check for Referential Integrity Violations
Copy
15. Update a Column Based on a Condition
Copy
16. Find Orders Where the Total Amount Exceeds the User’s Average
Copy
17. Generate All Possible Pairs of Users
Copy
18. Find Users Who Made More Than One Order in a Day
Copy
19. Calculate Time Difference Between Orders
Copy
20. Dynamic Query with Aggregates
Copy
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