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
customerstable - 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;
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
