We need to practice various SQL SELECT queries on a puppies database table that contains information about different dogs. The table includes columns for id, name, age_yrs, breed, weight_lbs, and microchipped status. We need to write different types of SELECT statements to retrieve specific data based on various conditions.
Key information about the table structure:
CREATE TABLE puppies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(100),
age_yrs DECIMAL(2,1),
breed VARCHAR(100),
weight_lbs INT,
microchipped BOOLEAN
);
-- Select everything from the puppies table
SELECT *
FROM puppies;
-- Select only name, age, and weight
SELECT name, age_yrs, weight_lbs
FROM puppies;
-- Select specific puppy with id = 5
SELECT name, age_yrs, weight_lbs
FROM puppies
WHERE id = 5;
-- Select all microchipped puppies
SELECT name, age_yrs, weight_lbs
FROM puppies
WHERE microchipped = 1;
-- Select puppies over 25 pounds
SELECT name, age_yrs, weight_lbs
FROM puppies
WHERE weight_lbs > 25;
-- Select large microchipped puppies
SELECT name, age_yrs, weight_lbs
FROM puppies
WHERE weight_lbs > 25 AND microchipped = 1;
-- Using table and column aliases for cleaner output
SELECT
p.name AS puppy_name,
p.age_yrs AS age,
p.weight_lbs AS weight
FROM puppies p
WHERE p.weight_lbs > 25
AND p.microchipped = 1;
-- Using BETWEEN for range queries
SELECT name, age_yrs, weight_lbs
FROM puppies
WHERE weight_lbs BETWEEN 25 AND 70
AND microchipped = 1;
SELECT statements are like asking questions to a database. Think of the puppies table as a spreadsheet, and SELECT queries as different ways to filter and view that spreadsheet.
Let's break down each component:
SELECT: This specifies which columns you want to see. It's like choosing which spreadsheet columns to keep visible while hiding others.
FROM: This tells the database which table to query. In a library analogy, this would be like specifying which shelf to look at.
WHERE: This filters the rows based on conditions. It's similar to using filters in a spreadsheet or online shop to show only items meeting certain criteria.
These SQL concepts are used everywhere in the real world:
E-commerce: Finding products by price range, category, or availability.
Social Media: Displaying posts from users you follow or posts with specific hashtags.
Banking: Finding transactions within a date range or above a certain amount.
Remember that:
Try these additional queries: