11
sql
-- Create the students table
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT,
grade INTEGER
);
-- Insert sample data
INSERT INTO students (name, grade) VALUES
('Alex', 92),
('Sara', 85),
('Daniel', 90),
('Mia', 78),
('John', 95);
-- 1. Create an index for faster lookups by name
CREATE INDEX idx_student_name ON students(name);
-- 2. Avoid SELECT *: fetch only needed columns
SELECT name, grade FROM students;
-- 3. Use EXPLAIN QUERY PLAN to analyze performance
EXPLAIN QUERY PLAN
SELECT name FROM students WHERE grade > 90;
-- 4. Example of filtering before joining
SELECT *
FROM (SELECT * FROM orders WHERE status = 'paid') AS o
JOIN customers c ON o.customer_id = c.id;
-- 5. Use LIMIT during development
SELECT * FROM orders LIMIT 100;
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.