Hey there 👋
Hope you are doing well 🙂
In the last blog we have discussed about SQL operators ,wildcards and aggragate functions. In this blog we are going to see ALTER command and its use cases, Order By and NULL Values.
ALTER Command in SQL
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. It is a DDL command.
Use case 1 -:
Add a column in table
Suppose we have table that contains record of Students, their Name, Roll_no and IQ. Here is the table -:
Now here we need to add one more column to table and i.e. Grade column. Here's how we can do it -:
So initially the column will contain NULL values as we have not specified any default value here.
Now the important thing is how can we add data into this as per our existing data?
So this is a tricky part here we can either introduce a formula and assign grades to each student as per that formula.
So here Update is used to update data in table then we have used Set command to set the value of Grade column according to different Cases. It works more like a Switch statement whenever it encounters a valid case then that value is of Grade is set.
But what if you have enormous data and each column has a unique entry in that case we automate the process using external scripts or we use joins.
Use case 2 -:
Drop a column in table
Now let's drop Grades column from our table.
Note that using the DROP statement, the objects are permanently deleted or lost from a database, and they cannot be rolled back.
Use Case 3 -:
Rename a column in table
Let's remane Roll_no column and change it to "Id".
Order By Statement in SQL
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
We can sort our Student table based on IQ of students (ascending order)-:
Here "ASC" refers ascending order.
For descending order we will simply use "DESC".
We can also order by multiple columns. For example we sort above table both on the basis of Id and IQ. So this is how we can do it.
NULL Values in SQL
An empty field in SQL which is not being explicitly initialized is set to NULL. As you have seen above when we added "Grade" column to our table, it was empty initially.
Is Null operator
The IS NULL operator is used to test for empty values (NULL values).
Is Not Null operator
The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).
So these were Alter command, Order By and Null values in SQL.
In the next blog we will see keys and joins.
I hope you liked my blog. Please leave some ❤ and don't forget to follow me.
Also have you checked my Instagram page where I upload necessary resources for software engineers.
If not check it out here -:
👉 Instagram: https://www.instagram.com/fluxx_96/
👉 YouTube: https://www.youtube.com/@Fluxx-n4q/shorts
Thankyou 💙
Top comments (0)