
For an instructor lead, in-depth look at learning SQL click below.
Structured Query Language (SQL) forms the cornerstone of most operations performed on a database — from data creation and modification, to data management and reporting. Today, we get our hands dirty with database queries, starting from the very basics.
What is SQL?
SQL is a standardized programming language that databases use to manage data. It offers a platform to run queries, manipulate data sets, and permit adequate interaction with the database, in a set-based, declarative manner. Comes in variants, like MySQL, SQL Server and PostgreSQL, but the core commands remain the same across the board.
First Things First: The SELECT Statement
When it comes to SQL, the most commonly used statement is SELECT. With SELECT, you can retrieve data from a single table or multiple tables joined together.
1 2 3 4 |
-- Select all columns from a single table SELECT * FROM Employees; |
In the above example, the “*” tells SQL to return all columns from the table “Employees”.
Specifying Column Names
Better practice than selecting everything in a table is to get specific about the columns you require from a database.
1 2 3 4 |
-- Select specific columns from a single table SELECT EmployeeID, FirstName, LastName FROM Employees; |
In this case, only the columns “EmployeeID”, “FirstName”, and “LastName” will be returned from the “Employees” table.
WHERE Clause – Filtering Records
SQL lets you filter the records using the WHERE clause. Here’s an example:
1 2 3 4 |
-- Select records with condition SELECT EmployeeID, FirstName, LastName FROM Employees WHERE EmployeeID = 5; |
Here, only the record where EmployeeID is 5 will be returned. You can use comparison operators like =, <>, >, <, >=, <= for precise record selection.
Conclusion
This was a basic introduction to retrieving data with SQL. There’s a lot more to cover – sorting, grouping, aggregate functions, subqueries and much more. Practice these examples, for now, explore a little bit more, and stay tuned for the next lessons where we’ll delve deeper into the world of SQL.