Beginner's Guide to SQL
If you're new to SQL (Structured Query Language) as I am, you're in the right place. In this post, I intend to cover the basics of SQL that got me started to help you kickstart your learning journey as well.
First things first, we start with definitions: What is SQL? SQL is an abbreviation for Structured Query Language and it can be used to store, organize and analyze data. We are able to do this by running queries that inform what to be executed by SQL, more like us communicating with the database through SQL and the queries are the language understood by SQL.
Store
One of the uses of SQL as mentioned above is to store data. With SQL, we can create and store structured data (data that’s stored in columns and rows: tabulated). How do we create a table you ask? This is done as follows:
CREATE TABLE Student;
The above command creates a table named ‘Student’. A table is incomplete without columns. To add columns we do as below;
CREATE TABLE Student(
student_id INT,
name VARCHAR(20),
major VARCHAR(20)
);
Above we’ve added columns titled student_id, name and major to our table Student. The value ‘INT’ specifies the data type to be used in the column student_id. INT is short form for integer. We’ll cover data types later on. ‘VARCHAR’ specifies the number of characters to be used. The number in brackets is the maximum number of characters to be used.
Next we should be able to add data to the table, the student details i.e. This is done as follows;
INSERT INTO student VALUES(1, 'Jack', 'Biology');
INSERT INTO student VALUES(2, 'Mike', 'Computer Science');
INSERT INTO student VALUES(3, 'Kate', 'Sociology');
INSERT INTO students VALUES(4, 'Jack', 'Biology');
INSERT INTO students VALUES(5, 'Claire', 'Computer Science');
INSERT INTO students VALUES(6, 'Justine', 'Data Analytics');
Running the above query returns;
We have created our first table (student) using SQL and stored data in it.
If we have a student that hasn’t decided on what major to pursue, we can add their details as follows;
INSERT INTO student(student_id, name) VALUES(7, 'Ojoda');
Running this updates the table student to return the following:
We can see that the student named Ojoda’s major returns as null. this is because in the query above, we specified the values to input, which were student_id and name.
There’s one key statement that we haven’t covered and that is SELECT. This is used to select data from a database. For me to get the results attached in the above images I had to run the SELECT statement. This statement is used together with FROM and WHERE statements. To display what the students table contents we run the query below;
SELECT * FROM student;
SELECT * is a statement that selects everything, commonly referred to as ‘select all’. From the above example we are selecting everything from the table students.
Suppose we want to filter our dataset and return students whose major is Biology, we can achieve this by using another statement: WHERE. We can do this as shown below:
SELECT * FROM student,
WHERE major = 'Biology';
In other words the above query can be read as ‘Select everything from the table student where in the major column, the value is ‘Biology’.’
This will only return data of students whose major is Biology as shown below;
It is important to note that we include a semi colon (;) at the end of each SQL statement.
I hope this brief blog helps you get an understanding of SQL.