Lesson 13 - Databases
Single-Table SELECT Statements
- SELECT * FROM CUSTOMERS;
- SELECT CustomerID, FirstName, LastName FROM CUSTOMERS;
- SELECT * FROM CUSTOMERS WHERE LastName = 'Smith';
- SELECT * FROM CUSTOMERS ORDER BY LastName ASC;
- SELECT * FROM PRODUCTS WHERE Price > 50;
- SELECT COUNT(*) FROM ORDERS;
INSERT, UPDATE, and DELETE Statements
- INSERT INTO CUSTOMERS (FirstName, LastName, Email) VALUES ('Jane', 'Doe', 'jane@example.com');
- INSERT INTO PRODUCTS (ProductName, Price, Stock) VALUES ('Widget', 9.99, 100);
- UPDATE CUSTOMERS SET Email = 'new@example.com' WHERE CustomerID = 1;
- UPDATE PRODUCTS SET Price = 12.99 WHERE ProductName = 'Widget';
- DELETE FROM CUSTOMERS WHERE CustomerID = 5;
- DELETE FROM ORDERS WHERE OrderDate < '2024-01-01';
What I Learned
In this lesson I learned about databases and how to use SQL to work with them. SQL lets you get information out of a database using SELECT statements. You can also add new records with INSERT, change existing ones with UPDATE, and remove them with DELETE. I also learned that it is important to use a WHERE clause with UPDATE and DELETE so you don't accidentally change all the rows in a table. I can use this in the future if I build a website that needs to store information like user accounts or a list of products.