Skip to content

Instantly share code, notes, and snippets.

@ekamwalia
Last active June 6, 2020 12:03
Show Gist options
  • Save ekamwalia/9542ce3851a32c51388b0639e52e949b to your computer and use it in GitHub Desktop.
Save ekamwalia/9542ce3851a32c51388b0639e52e949b to your computer and use it in GitHub Desktop.
SQL 101

SQL

SQL is used to store data and use data. All data in SQL is stored in the form of tables(think excel spreadsheet) and a group of tables is called a database.

Every table has rows and columns. THe columns represent the properties like Name, Age, Email etc. the rows contain the values for these properties.

Example

mysql> select * from profile;
+-------+-----------------+------+
| name  | email           | age  |
+-------+-----------------+------+
| ekam  | [email protected]  |   21 |
| anhad | [email protected] |   21 |
+-------+-----------------+------+
2 rows in set (0.00 sec)

Ignore the first line for a minute. The table has 3 columns name, email, age. It has 2 rows and each row has details about one person. A row is also called a tuple or a record.

WHy use SQL instead of Excel? SQL allows you to use the data "programatically" using a language called Structured Query Language or SQL. Let's see one sample query.

The name of the table I created above is profile. So lets try to get all the tuples in the profile table.

First, we need to tell SQL fields(columns) from the table should be selected. For this we use the SELECT keyword. so the query looks something like

SELECT name, email, age

Now we need to tell SQL which table to get this information from. For this we use the FROM keyword.

SELECT name, email, age
FROM profile

If we run this query in SQL, the result is https://imgur.com/a/O26uyYL

Now if we need to select only the record for ekam, we can do that by using the WHERE command.

SELECT name, email, age
FROM profile
WHERE name = 'ekam'

Result - https://imgur.com/a/etjE3bv

Quick facts

  • SQL commands are case insensitive. SELECT select, sae thing
  • All SQL commands should end with ;
  • You can use SELECT * if you want to select all the columns

If you did not understand everything so far, read it again. or ask me. If you understood, go to this website next for interactive SQL lessons - https://sqlbolt.com/ Or watch this tutorial - https://www.youtube.com/watch?v=9ylj9NR0Lcg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment