Inventory Management - SQL Typing CST Test
Loading…
Inventory Management — SQL Code
Tracks stock levels, suppliers, inventory movement, and reorder alerts.
CREATE TABLE suppliers (
supplier_id SERIAL PRIMARY KEY,
supplier_name VARCHAR(100)
);
CREATE TABLE inventory (
item_id SERIAL PRIMARY KEY,
item_name VARCHAR(100),
stock_quantity INTEGER,
reorder_level INTEGER,
supplier_id INTEGER REFERENCES suppliers(supplier_id)
);
INSERT INTO suppliers (supplier_name) VALUES
('Tech Supply Co'),
('Global Parts Ltd');
INSERT INTO inventory (
item_name,
stock_quantity,
reorder_level,
supplier_id
) VALUES
('SSD Drive', 50, 20, 1),
('RAM Module', 15, 25, 1),
('Power Supply', 10, 15, 2);
SELECT
item_name,
stock_quantity,
reorder_level,
CASE
WHEN stock_quantity < reorder_level THEN 'REORDER'
ELSE 'OK'
END AS inventory_status
FROM inventory;
SELECT
s.supplier_name,
COUNT(i.item_id) AS supplied_items
FROM suppliers s
JOIN inventory i
ON s.supplier_id = i.supplier_id
GROUP BY s.supplier_name;
CREATE INDEX idx_inventory_stock
ON inventory(stock_quantity);
SELECT *
FROM inventory
ORDER BY stock_quantity ASC;SQL Language Guide
SQL (Structured Query Language) is a standard language for managing and manipulating relational databases, enabling querying, insertion, updating, and deletion of data efficiently.
Primary Use Cases
- ▸Querying relational data for applications
- ▸Data aggregation and reporting
- ▸Transaction management in business systems
- ▸Analytics and business intelligence
- ▸Database schema definition and data integrity enforcement
Notable Features
- ▸Declarative syntax for querying data
- ▸Support for CRUD operations: Create, Read, Update, Delete
- ▸Joins and subqueries for combining data
- ▸Transactions to ensure atomicity and consistency
- ▸Data definition language (DDL) for schema management
Origin & Creator
Developed at IBM in the early 1970s by Donald D. Chamberlin and Raymond F. Boyce.
Industrial Note
SQL is ubiquitous in enterprise, analytics, web applications, and data-intensive domains where structured relational data is used.
Quick Explain
- ▸SQL provides declarative commands to interact with relational database systems (RDBMS).
- ▸Supports querying, filtering, aggregation, joins, and transactions.
- ▸Used across major RDBMS like MySQL, PostgreSQL, Oracle, SQL Server, and SQLite.
- ▸Enforces schema, data types, constraints, and relationships.
- ▸Enables both simple queries and complex analytical operations.
Core Features
- ▸SELECT queries with filtering and ordering
- ▸INSERT, UPDATE, DELETE operations
- ▸JOINs (INNER, LEFT, RIGHT, FULL) and subqueries
- ▸GROUP BY, HAVING, and aggregate functions
- ▸Transactions and constraint enforcement
Learning Path
- ▸Understand relational data modeling
- ▸Learn basic CRUD operations
- ▸Master joins, subqueries, and aggregation
- ▸Study indexing, constraints, and transactions
- ▸Practice database optimization and analytics queries
Practical Examples
- ▸SELECT data with WHERE filters
- ▸JOIN multiple tables for relational queries
- ▸Aggregate data using SUM, COUNT, AVG, MAX, MIN
- ▸Create and manage tables with constraints
- ▸Use transactions to ensure atomic operations
Comparisons
- ▸SQL vs NoSQL: SQL for structured data, NoSQL for flexible schema/unstructured data
- ▸MySQL vs PostgreSQL: MySQL widely used, PostgreSQL more advanced features
- ▸SQL vs GraphQL: SQL queries relational data, GraphQL queries API endpoints
- ▸SQL vs ORM query languages: SQL native and powerful, ORM abstracts complexity
- ▸SQL vs Excel: SQL handles large datasets efficiently, Excel for small-scale analysis
Strengths
- ▸Standardized and widely supported across RDBMS
- ▸Powerful for structured data manipulation
- ▸Enables complex queries and analytics
- ▸ACID-compliant transactions ensure data reliability
- ▸Strong community and documentation support
Limitations
- ▸Less flexible for unstructured or hierarchical data
- ▸Complex queries can be hard to optimize
- ▸Performance depends on indexing and schema design
- ▸Portability issues with vendor-specific SQL extensions
- ▸Limited in handling very large-scale distributed data compared to NoSQL
When NOT to Use
- ▸For unstructured or schema-less data (use NoSQL)
- ▸When horizontal scaling of huge datasets is primary concern
- ▸Rapid prototyping of small, transient datasets
- ▸Applications requiring real-time streaming analytics only
- ▸When fully distributed databases are preferred
Cheat Sheet
- ▸SELECT * FROM table WHERE condition - basic query
- ▸INSERT INTO table (columns) VALUES (values) - add data
- ▸UPDATE table SET column=value WHERE condition - modify data
- ▸DELETE FROM table WHERE condition - remove data
- ▸CREATE TABLE table_name (columns) - define schema
FAQ
- ▸Is SQL open-standard? -> Yes, ANSI/ISO standard.
- ▸Does SQL work on all RDBMS? -> Core syntax is standard, but extensions vary.
- ▸Can SQL handle large datasets? -> Yes, with indexing and optimization.
- ▸Is SQL suitable for unstructured data? -> No, consider NoSQL for that.
- ▸How do you prevent SQL injection? -> Use parameterized queries and prepared statements.
30-Day Skill Plan
- ▸Week 1: Basic SELECT, INSERT, UPDATE, DELETE
- ▸Week 2: Joins, GROUP BY, and aggregate functions
- ▸Week 3: Subqueries, CTEs, and window functions
- ▸Week 4: Transactions, constraints, and indexing
- ▸Week 5: Performance tuning and backup strategies
Final Summary
- ▸SQL is the standard language for relational database management.
- ▸Enables querying, updating, and managing structured data.
- ▸Supports transactions, constraints, and complex joins.
- ▸Widely adopted in enterprise, analytics, and web applications.
- ▸Works with most RDBMS, with strong community and tooling support.
Project Structure
- ▸Database instance
- ▸Schemas for logical separation
- ▸Tables representing entities
- ▸Views for reusable queries
- ▸Stored procedures/functions for business logic
Monetization
- ▸Many RDBMS are open-source (MySQL, PostgreSQL, SQLite)
- ▸Enterprise editions (Oracle, SQL Server) provide advanced features
- ▸Core SQL skills valuable in IT, data, and analytics jobs
- ▸Optimized SQL improves application performance
- ▸Foundation for business intelligence and reporting
Productivity Tips
- ▸Use parameterized queries for security
- ▸Index frequently queried columns
- ▸Normalize data to reduce redundancy
- ▸Use views and stored procedures for reusable logic
- ▸Profile queries regularly for performance
Basic Concepts
- ▸Database - container for related tables
- ▸Table - collection of rows with defined columns
- ▸Row/Record - single entry in a table
- ▸Column/Field - attribute of a table with data type
- ▸Primary/Foreign Keys - enforce uniqueness and relationships
Official Docs
- ▸https://www.iso.org/standard/63555.html (SQL standard)
- ▸MySQL Documentation
- ▸PostgreSQL Documentation
- ▸Oracle SQL Reference
- ▸SQL Server Docs
More SQL Typing Exercises
PostgreSQL Advanced QueriesSQL Employee Management SystemSQL Employee Management SystemSQL Customer SegmentationSQL Customer SegmentationSQL Financial Transactions AnalysisSQL Banking System QueriesSQL Order Processing WorkflowSQL Order Processing WorkflowSQL Order Processing WorkflowSQL Healthcare Records ManagementSQL University Management System