
For an instructor lead, in-depth look at learning SQL click below.
If you’re new to data manipulation and data analysis, then learning SQL is a critical step in expanding your skills. SQL, or Structured Query Language, is a language designed to manage data stored in relational databases.
Why Learn SQL?
SQL lets you access and manipulate databases. It can be used to execute queries against a database, retrieve data from a database, insert records in a database, update records in a database, delete records from a database, and create new databases and tables in a database.
Beginning with SQL
Here’s the most basic SQL command:
1 2 3 |
SELECT * FROM table_name; |
This command selects all columns from a certain table (replace ‘table_name’ with actual name of the table). The asterisk (*) is a wildcard character that means ‘everything’. This is a good starting point to view all data in a table.
Filtering Data with SQL
Now let’s say you want to filter this data and only view certain rows. We can use the WHERE clause for this:
1 2 3 |
SELECT * FROM table_name WHERE column_name = 'value'; |
With this command, SQL will only return the rows where ‘column_name’ is equal to ‘value’.
Sorting Data with SQL
If you want to sort your results, use the ORDER BY keyword:
1 2 3 |
SELECT * FROM table_name ORDER BY column_name ASC; |
This sorts the results in ascending order based on the column name. If you want it in descending order, simply replace ASC with DESC.
Aggregating Data with SQL
You can use functions like COUNT(), MAX(), MIN(), SUM(), AVG() to get aggregate data about a column:
1 2 3 |
SELECT COUNT(column_name) FROM table_name; |
This will give you the number of rows that have a value in ‘column_name’.
JOINing tables
The JOIN is used to combine rows from two or more tables, based on a related column between them. Here is a simple JOIN SQL Query:
1 2 3 4 5 6 |
SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name; |
This ends our brief intro to SQL! The learning doesn’t stop here. There’s much more to SQL than this brief intro. Remember, the best way to learn is by doing. Continually practice and challenge yourself. Good luck and happy querying!
Conclusion
SQL is a versatile language that is fundamental to dealing with data and databases. By mastering the SQL basics described in this blog post, you are setting yourself up for success in your data analysis career. As you get comfortable with these concepts, challenge yourself by learning more complex commands and operations.