If you’re just getting started with SQL, it can feel overwhelming to know where to begin. The good news? Writing your first queries is simpler than you think. In this beginner SQL tutorial, we’ll go through ten of the most common SQL query examples that every new learner should practice. By the end, you’ll not only understand how to write SQL queries, you’ll also feel confident applying them in real projects. Interactive SQL Sandbox Click the button below to open the SQL Sandbox. Copy any of the queries from this article and paste them into the editor. Press Run Query to see how the database responds. It’s the easiest way to learn by doing. SQL Playground > Contents 1. Select All Data from a Table SELECT *FROM customers; This query retrieves every column and row from the customers table. It’s the simplest way to look at everything inside a dataset. 2. Select Specific Columns SELECT first_name, last_name, emailFROM customers; Instead of pulling all the data, you can target only the columns you need. This keeps results clean and focused. 3. Filter Rows with WHERE SELECT *FROM orders WHERE order_date = '2025-09-01'; The WHERE clause filters results. Here, we’re only seeing orders placed on a specific date. 4. Sort Results with ORDER BY SELECT *FROM products ORDER BY price DESC; Use ORDER BY to sort data. In this case, we’re showing products in descending order of price—most expensive first. 5. Limit the Number of Rows SELECT *FROM customers LIMIT 5; Sometimes you just need a quick sample. LIMIT keeps your results manageable by returning only a set number of rows. 6. Count Rows SELECT COUNT(*)FROM orders; The COUNT() function tells you how many rows exist in a table. A quick way to measure table size or query results. 7. Find Unique Values with DISTINCT SELECT DISTINCT countryFROM customers; DISTINCT removes duplicates. This is useful when you want to see unique entries, like how many different countries your customers are from. 8. Use Aliases for Clarity SELECT first_name AS fname, last_name AS lnameFROM customers; Aliases (AS) let you rename columns in the result set, making outputs easier to read or shortening long names. 9. Filter with Multiple Conditions SELECT *FROM orders WHERE amount > 100 AND status = 'completed'; Combine conditions with AND or OR to refine results. Here, we’re finding only completed orders worth more than $100. 10. Combine Data with a JOIN SELECT orders.id, customers.first_name, customers.last_nameFROM orders JOIN customers ON orders.customer_id = customers.id; The JOIN command lets you combine data from multiple tables—one of SQL’s most powerful features. Final ThoughtsThese 10 SQL query examples form the foundation for working with databases. Practice them until you’re comfortable, and you’ll be ready to tackle more advanced topics like grouping, aggregations, and subqueries. FAQs About Learning SQL 1. Is SQL easy for beginners to learn? 2. What’s the best way to practice writing SQL queries? 3. Do I need to know programming before learning SQL? 4. What are the most important SQL commands for beginners? 5. How long does it take to become good at SQL? 👉 Build real-world database skills in our SQL Basics course. https://teamtreehouse.com/tracks/beginning-sql (责任编辑:) |