Write a query to fetch only the first 10 records from the customers table.
Sure! Let’s walk through how to fetch the first 10 records from a table named customers
, including:
- Creating the
customers
table - Inserting sample data
- Writing the query to fetch only the first 10 records
- Showing expected output
Step 1: Create the customers
Table
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
city VARCHAR(50),
created_at DATE
);
Step 2: Insert Dummy Data
Here’s a sample SQL INSERT
statement to add 15 dummy records:
INSERT INTO customers (id, name, email, city, created_at) VALUES
(1, 'John Smith', 'john@example.com', 'New York', '2023-01-01'),
(2, 'Alice Johnson', 'alice@example.com', 'Los Angeles', '2023-01-03'),
(3, 'Bob Lee', 'bob@example.com', 'Chicago', '2023-01-05'),
(4, 'Mary Jane', 'mary@example.com', 'Houston', '2023-01-07'),
(5, 'Tom Hardy', 'tom@example.com', 'Phoenix', '2023-01-09'),
(6, 'Emma Watson', 'emma@example.com', 'Philadelphia', '2023-01-11'),
(7, 'Liam Brown', 'liam@example.com', 'San Antonio', '2023-01-13'),
(8, 'Olivia Davis', 'olivia@example.com', 'San Diego', '2023-01-15'),
(9, 'Noah Wilson', 'noah@example.com', 'Dallas', '2023-01-17'),
(10, 'Ava Martinez', 'ava@example.com', 'San Jose', '2023-01-19'),
(11, 'Ethan Miller', 'ethan@example.com', 'Austin', '2023-01-21'),
(12, 'Sophia Taylor', 'sophia@example.com', 'Jacksonville', '2023-01-23'),
(13, 'Mason Anderson', 'mason@example.com', 'Fort Worth', '2023-01-25'),
(14, 'Isabella Thomas', 'isabella@example.com', 'Columbus', '2023-01-27'),
(15, 'James Moore', 'james@example.com', 'Charlotte', '2023-01-29');
Step 3: Write the Query to Fetch First 10 Records
To fetch only the first 10 records, use the LIMIT
clause:
SELECT * FROM customers
LIMIT 10;
This works in MySQL, PostgreSQL, SQLite, etc.
Alternative (for SQL Server)
If you're using SQL Server, use:
SELECT TOP 10 * FROM customers;
Step 4: Expected Output
This query will return the first 10 rows (based on insert order unless explicitly sorted):
id | name | city | created_at | |
---|---|---|---|---|
1 | John Smith | john@example.com | New York | 2023-01-01 |
2 | Alice Johnson | alice@example.com | Los Angeles | 2023-01-03 |
3 | Bob Lee | bob@example.com | Chicago | 2023-01-05 |
4 | Mary Jane | mary@example.com | Houston | 2023-01-07 |
5 | Tom Hardy | tom@example.com | Phoenix | 2023-01-09 |
6 | Emma Watson | emma@example.com | Philadelphia | 2023-01-11 |
7 | Liam Brown | liam@example.com | San Antonio | 2023-01-13 |
8 | Olivia Davis | olivia@example.com | San Diego | 2023-01-15 |
9 | Noah Wilson | noah@example.com | Dallas | 2023-01-17 |
10 | Ava Martinez | ava@example.com | San Jose | 2023-01-19 |
Pro Tip: Always Use ORDER BY
If you want consistent results (e.g., latest or oldest first), use:
SELECT * FROM customers
ORDER BY created_at ASC
LIMIT 10;
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.