{"appId":"sql-app","version":1551,"selectedAngularVersion":20,"item":{"id":"lesson-introduction-to-databases","conceptKey":"introduction-to-databases","subjectId":"subject-introduction-to-databases","title":"Introduction to Databases","summary":"A database stores related data in an organized form so applications can create, read, update, and protect it reliably.","baseContent":"<h2>Introduction to Databases</h2><p>A database stores related data in an organized form so applications can create, read, update, and protect it reliably.</p><h3>Example</h3><pre><code>CREATE DATABASE shop;</code></pre><h3>Key point</h3><p>Use the smallest correct statement, test it with representative data, and verify constraints and performance before production use.</p><h3>Real-life example</h3><p>An online shop stores customers, products, orders, and order items. The schema must prevent duplicate identities, missing required values, and orders that reference customers that do not exist.</p><h3>Advanced example</h3><pre><code>BEGIN;\n-- Preview the exact target set first.\nSELECT id FROM orders WHERE status = 'pending';\n-- Apply the Introduction to Databases operation, verify affected rows, then commit.\nCOMMIT;</code></pre><h3>Expected result</h3><p>The schema or operation satisfies the stated rule, rejects invalid data, and can be verified with a repeatable query.</p><h3>Production check</h3><ul><li>Test with empty, duplicate, null, and boundary values.</li><li>Use a transaction for related writes.</li><li>Inspect the execution plan before adding an index.</li><li>Use parameterized queries for application input.</li></ul><h3>PicoStore practice database</h3><p>Every lesson in this SQL course uses the same fictional e-commerce database: <strong>picostore</strong>. Create it once, then reuse its tables throughout the course.</p><pre><code>CREATE DATABASE picostore;\n\nCREATE TABLE customers (\n  customer_id INTEGER PRIMARY KEY,\n  name VARCHAR(100) NOT NULL,\n  email VARCHAR(255) UNIQUE NOT NULL,\n  city VARCHAR(80),\n  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE products (\n  product_id INTEGER PRIMARY KEY,\n  name VARCHAR(120) NOT NULL,\n  category VARCHAR(60),\n  price DECIMAL(10, 2) CHECK (price &gt;= 0),\n  stock INTEGER DEFAULT 0\n);\n\nCREATE TABLE orders (\n  order_id INTEGER PRIMARY KEY,\n  customer_id INTEGER NOT NULL REFERENCES customers(customer_id),\n  status VARCHAR(20) DEFAULT 'pending',\n  total DECIMAL(12, 2) NOT NULL,\n  ordered_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE order_items (\n  order_id INTEGER REFERENCES orders(order_id),\n  product_id INTEGER REFERENCES products(product_id),\n  quantity INTEGER CHECK (quantity &gt; 0),\n  unit_price DECIMAL(10, 2) NOT NULL,\n  PRIMARY KEY (order_id, product_id)\n);\n\nCREATE TABLE employees (\n  employee_id INTEGER PRIMARY KEY,\n  manager_id INTEGER REFERENCES employees(employee_id),\n  name VARCHAR(100) NOT NULL,\n  department VARCHAR(60),\n  salary DECIMAL(12, 2)\n);\n\nCREATE TABLE accounts (\n  account_id INTEGER PRIMARY KEY,\n  customer_id INTEGER REFERENCES customers(customer_id),\n  balance DECIMAL(14, 2) NOT NULL\n);</code></pre><h3>Starter data</h3><pre><code>INSERT INTO customers (customer_id, name, email, city) VALUES\n  (1, 'Asha', 'asha@example.com', 'Pune'),\n  (2, 'Ravi', 'ravi@example.com', 'Mumbai'),\n  (3, 'Meera', 'meera@example.com', NULL);\n\nINSERT INTO products (product_id, name, category, price, stock) VALUES\n  (101, 'Keyboard', 'Accessories', 2499.00, 12),\n  (102, 'Monitor', 'Displays', 14999.00, 5),\n  (103, 'USB Cable', 'Accessories', 499.00, 0);\n\nINSERT INTO orders (order_id, customer_id, status, total, ordered_at) VALUES\n  (1001, 1, 'paid', 15498.00, '2026-01-10 10:00:00'),\n  (1002, 1, 'pending', 499.00, '2026-01-12 12:30:00'),\n  (1003, 2, 'shipped', 2499.00, '2026-02-02 09:15:00');\n\nINSERT INTO order_items (order_id, product_id, quantity, unit_price) VALUES\n  (1001, 102, 1, 14999.00), (1001, 103, 1, 499.00),\n  (1002, 103, 1, 499.00), (1003, 101, 1, 2499.00);</code></pre><h3>Another practical example</h3><pre><code>SELECT o.order_id, c.name AS customer, o.status, o.total\nFROM orders o\nJOIN customers c ON c.customer_id = o.customer_id\nWHERE o.total &gt;= 1000\nORDER BY o.ordered_at DESC;</code></pre><h3>Check the result</h3><p>Run the verification query, compare the returned rows with the starter data, and explain why every included or excluded row is correct.</p><section data-nonversioned-curriculum=\"1\"><h3>Easy example</h3><p>Start with a small customer table and retrieve active customers in a predictable order.</p><pre><code>SELECT customer_id, name, email\nFROM customers\nWHERE status = 'active'\nORDER BY name;</code></pre><h3>How to verify the easy example</h3><ul><li>Run it with representative input.</li><li>Confirm the expected output.</li><li>Try one missing, invalid, or boundary value.</li></ul><h3>Advanced example</h3><p>Use a CTE and a window function to rank customer revenue while keeping the query readable and testable.</p><pre><code>WITH customer_revenue AS (\n  SELECT customer_id, SUM(total_amount) AS revenue\n  FROM orders\n  WHERE order_status = 'completed'\n  GROUP BY customer_id\n)\nSELECT customer_id, revenue,\n       DENSE_RANK() OVER (ORDER BY revenue DESC) AS revenue_rank\nFROM customer_revenue\nORDER BY revenue_rank, customer_id;</code></pre><h3>Advanced review</h3><ul><li>Explain the tradeoffs and assumptions.</li><li>Test failure, scale, security, and recovery behavior.</li><li>Capture evidence from tests, execution plans, logs, or review output.</li></ul><h3>Additional practical guidance</h3><div><h3>Introduction to Databases: MySQL and PostgreSQL</h3><p>Model the rule with keys and constraints instead of relying only on application code. MySQL and PostgreSQL support the core relational design, but generated-column, check-constraint, and alteration details can differ by server version.</p><h3>Required verification</h3><ul><li>Run the simple case.</li><li>Test a NULL, duplicate, empty, or boundary case where relevant.</li><li>Confirm the affected rows or query result.</li><li>Use EXPLAIN for performance-sensitive queries.</li></ul></div></section>","detailId":"lesson-introduction-to-databases-698e3751","versions":[],"isActive":true,"detailIsActive":true,"lessonVersions":[],"selectedVersion":20,"content":"","updatedAt":"2026-08-01T09:43:04.474Z","details":[{"id":"lesson-introduction-to-databases","conceptKey":"introduction-to-databases","subjectId":"subject-introduction-to-databases","title":"Introduction to Databases","summary":"A database stores related data in an organized form so applications can create, read, update, and protect it reliably.","baseContent":"<h2>Introduction to Databases</h2><p>A database stores related data in an organized form so applications can create, read, update, and protect it reliably.</p><h3>Example</h3><pre><code>CREATE DATABASE shop;</code></pre><h3>Key point</h3><p>Use the smallest correct statement, test it with representative data, and verify constraints and performance before production use.</p><h3>Real-life example</h3><p>An online shop stores customers, products, orders, and order items. The schema must prevent duplicate identities, missing required values, and orders that reference customers that do not exist.</p><h3>Advanced example</h3><pre><code>BEGIN;\n-- Preview the exact target set first.\nSELECT id FROM orders WHERE status = 'pending';\n-- Apply the Introduction to Databases operation, verify affected rows, then commit.\nCOMMIT;</code></pre><h3>Expected result</h3><p>The schema or operation satisfies the stated rule, rejects invalid data, and can be verified with a repeatable query.</p><h3>Production check</h3><ul><li>Test with empty, duplicate, null, and boundary values.</li><li>Use a transaction for related writes.</li><li>Inspect the execution plan before adding an index.</li><li>Use parameterized queries for application input.</li></ul><h3>PicoStore practice database</h3><p>Every lesson in this SQL course uses the same fictional e-commerce database: <strong>picostore</strong>. Create it once, then reuse its tables throughout the course.</p><pre><code>CREATE DATABASE picostore;\n\nCREATE TABLE customers (\n  customer_id INTEGER PRIMARY KEY,\n  name VARCHAR(100) NOT NULL,\n  email VARCHAR(255) UNIQUE NOT NULL,\n  city VARCHAR(80),\n  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE products (\n  product_id INTEGER PRIMARY KEY,\n  name VARCHAR(120) NOT NULL,\n  category VARCHAR(60),\n  price DECIMAL(10, 2) CHECK (price &gt;= 0),\n  stock INTEGER DEFAULT 0\n);\n\nCREATE TABLE orders (\n  order_id INTEGER PRIMARY KEY,\n  customer_id INTEGER NOT NULL REFERENCES customers(customer_id),\n  status VARCHAR(20) DEFAULT 'pending',\n  total DECIMAL(12, 2) NOT NULL,\n  ordered_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE order_items (\n  order_id INTEGER REFERENCES orders(order_id),\n  product_id INTEGER REFERENCES products(product_id),\n  quantity INTEGER CHECK (quantity &gt; 0),\n  unit_price DECIMAL(10, 2) NOT NULL,\n  PRIMARY KEY (order_id, product_id)\n);\n\nCREATE TABLE employees (\n  employee_id INTEGER PRIMARY KEY,\n  manager_id INTEGER REFERENCES employees(employee_id),\n  name VARCHAR(100) NOT NULL,\n  department VARCHAR(60),\n  salary DECIMAL(12, 2)\n);\n\nCREATE TABLE accounts (\n  account_id INTEGER PRIMARY KEY,\n  customer_id INTEGER REFERENCES customers(customer_id),\n  balance DECIMAL(14, 2) NOT NULL\n);</code></pre><h3>Starter data</h3><pre><code>INSERT INTO customers (customer_id, name, email, city) VALUES\n  (1, 'Asha', 'asha@example.com', 'Pune'),\n  (2, 'Ravi', 'ravi@example.com', 'Mumbai'),\n  (3, 'Meera', 'meera@example.com', NULL);\n\nINSERT INTO products (product_id, name, category, price, stock) VALUES\n  (101, 'Keyboard', 'Accessories', 2499.00, 12),\n  (102, 'Monitor', 'Displays', 14999.00, 5),\n  (103, 'USB Cable', 'Accessories', 499.00, 0);\n\nINSERT INTO orders (order_id, customer_id, status, total, ordered_at) VALUES\n  (1001, 1, 'paid', 15498.00, '2026-01-10 10:00:00'),\n  (1002, 1, 'pending', 499.00, '2026-01-12 12:30:00'),\n  (1003, 2, 'shipped', 2499.00, '2026-02-02 09:15:00');\n\nINSERT INTO order_items (order_id, product_id, quantity, unit_price) VALUES\n  (1001, 102, 1, 14999.00), (1001, 103, 1, 499.00),\n  (1002, 103, 1, 499.00), (1003, 101, 1, 2499.00);</code></pre><h3>Another practical example</h3><pre><code>SELECT o.order_id, c.name AS customer, o.status, o.total\nFROM orders o\nJOIN customers c ON c.customer_id = o.customer_id\nWHERE o.total &gt;= 1000\nORDER BY o.ordered_at DESC;</code></pre><h3>Check the result</h3><p>Run the verification query, compare the returned rows with the starter data, and explain why every included or excluded row is correct.</p><section data-nonversioned-curriculum=\"1\"><h3>Easy example</h3><p>Start with a small customer table and retrieve active customers in a predictable order.</p><pre><code>SELECT customer_id, name, email\nFROM customers\nWHERE status = 'active'\nORDER BY name;</code></pre><h3>How to verify the easy example</h3><ul><li>Run it with representative input.</li><li>Confirm the expected output.</li><li>Try one missing, invalid, or boundary value.</li></ul><h3>Advanced example</h3><p>Use a CTE and a window function to rank customer revenue while keeping the query readable and testable.</p><pre><code>WITH customer_revenue AS (\n  SELECT customer_id, SUM(total_amount) AS revenue\n  FROM orders\n  WHERE order_status = 'completed'\n  GROUP BY customer_id\n)\nSELECT customer_id, revenue,\n       DENSE_RANK() OVER (ORDER BY revenue DESC) AS revenue_rank\nFROM customer_revenue\nORDER BY revenue_rank, customer_id;</code></pre><h3>Advanced review</h3><ul><li>Explain the tradeoffs and assumptions.</li><li>Test failure, scale, security, and recovery behavior.</li><li>Capture evidence from tests, execution plans, logs, or review output.</li></ul><h3>Additional practical guidance</h3><div><h3>Introduction to Databases: MySQL and PostgreSQL</h3><p>Model the rule with keys and constraints instead of relying only on application code. MySQL and PostgreSQL support the core relational design, but generated-column, check-constraint, and alteration details can differ by server version.</p><h3>Required verification</h3><ul><li>Run the simple case.</li><li>Test a NULL, duplicate, empty, or boundary case where relevant.</li><li>Confirm the affected rows or query result.</li><li>Use EXPLAIN for performance-sensitive queries.</li></ul></div></section>","detailId":"lesson-introduction-to-databases-698e3751","versions":[],"isActive":true,"detailIsActive":true,"lessonVersions":[],"selectedVersion":20,"content":"","updatedAt":"2026-08-01T09:43:04.474Z"}]}}