How to SELECT Data in SQL – Basic Queries Explained
How to SELECT Data in SQL – Basic Queries Explained
When you're working with databases, one of the most important things you'll do is retrieve data using SELECT queries. Whether you're checking that your data has been inserted correctly, or analyzing information, learning how to select data is a core SQL skill.
Let’s walk through several useful ways to SELECT data from a table.
1. Counting Rows in a Table
To count how many rows exist in a table, we use the COUNT(*) function. This is useful to quickly check how many records you have.
SELECT COUNT(*) FROM test;
Example Output:
+----------+
| count(*) |
+----------+
| 148 |
+----------+
1 row in set (0.00 sec)
This tells us that the test table currently contains 148 rows.
2. Selecting All Data from a Table
Once you've inserted data into a table, you’ll want to make sure it’s stored correctly. The most basic way to do this is to select all columns and rows using SELECT *.
SELECT * FROM test;
Example Output:
OfficeCode | AccountCode | AccountName | SecType
------------------------------------------------
1111 | 084534334 | Test | 1
1112 | 3434343435 | 3434 | 2
The * is a wildcard that means "select all columns." This query returns every column and row from the test table.
3. Limiting Results – Using TOP or LIMIT
Sometimes you only want to look at the first few rows of a table — especially if it’s large. You can limit your results to the top N rows.
- In SQL Server:
SELECT TOP 10 * FROM test;
- In MySQL or PostgreSQL:
SELECT * FROM test LIMIT 10;
Example Output:
OfficeCode | AccountCode | AccountName
-------------------------------------
6 | 6670708 | test
6 | 6610108 | test2
6 | 6044208 | test3
6 | 6045208 | test4
6 | 6049208 | test5
6 | 6046208 | test6
6 | 6050208 | test7
6 | 6047208 | test8
6 | 6048208 | test9
6 | 6041208 | test10
This gives you a quick peek at just the first 10 rows in the table. Very useful when you're debugging or testing new data loads.
4. Select Specific Columns
If you only need a few columns, you don’t have to retrieve everything. This makes queries faster and results easier to read.
SELECT OfficeCode, AccountName FROM test;
This will return just those two columns instead of the whole table.
More Coming Soon!
In the next post, we’ll dive into more advanced SELECT queries, including:
- Filtering rows using
WHERE - Using
ORDER BYto sort your results - Joining multiple tables together to build powerful queries
Keep experimenting, and happy querying!
Bookmark and share if this was helpful — and stay tuned for the next post in the series.
Comments
Post a Comment