SQL stands for Structured Query Language and we use SQL to interact with the database. Those interactions includes: create, modify and delete tables, view the data in a table, add/modify/delete rows from table and others.
On this blog, we will use MySQL to command our database. Let's say, we have a database table called "billionaires" table, that looks like following:
If you want to follow along, I am using SQL Online to create above table. You can also create one by running the following commands:
CREATE TABLE billionaires ( id INT(3), name VARCHAR(20), companyFounded INT(4), companyName VARCHAR(15) );
INSERT INTO billionaires VALUES (1, "Bill Gates", 1975, "Microsoft"), (2, "Jeff Bezos", 1995, "Amazon");
To view data in a database table, we use an SQL query
SELECT * FROM billionaires;
Let's say we only want to see the names of the billionaires. Let's do that using the query:
SELECT name FROM billionaires;
We don't have duplicate names on the table above. But if we did, we can remove the duplicates adding DISTINCT on above query.
SELECT DISTINCT name FROM billionaires;
Now, let's ask a question: "At what year, Microsoft was founded?"
To answer that, we can use the query:
SELECT companyfounded FROM billionaires WHERE companyname = "Microsoft";
Since our query is asking, what year? we want to return year column (companyFounded), so we
SELECT that column
WHERE companyname is Microsoft.
I recommend using double quotes, if we are targeting string values on our table rows. For example, on above query, we had "Microsoft" surrounded in double quotes.
Similarly, we can return values from two columns (name and companyFounded) using the query:
SELECT name, companyfounded FROM billionaires WHERE companyname = "Microsoft";
Let's talk about case sensitivity in SQL.
SQL keywords are not case sensitive. That means:
select * FROM billionaires;
is same as
SeLeCt * fRoM billionaires;
There are other keywords such as: WHERE, AS, ORDER BY, HAVING, GROUP BY, etc. In general, we use all the SQL keywords in capital letters.
For table and column names, depending on the setting, they can be case sensitive. I recommend using the same style as they are in the data tables.
I will continue adding more information on SQL on this blog soon. Let me know your thoughts!
Happy Blogging :)