Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

10 Essential SQL Commands and Functions Every Developer should learn

Hello guys, if you are starting with SQL and wondering which commands you should learn first then you have come at the right place. In this article, I have shared 10 most essential SQL commands and functions which I believe every programmer should learn. This includes commands to pull data from database as well write data into data, update data, and remove data from database. While writing in the SQL language, you will utilize an assortment of SQL keywords to make explanations and questions. An assertion includes a series of characters and SQL keywords that adjusts to the designing and grammar rules of the language and may influence information or control processes corresponding to your information. 

You can consider an assertion a "complete sentence" in the SQL language that can be effectively shipped off a data set administration framework.

A query mirrors an extraordinary kind of assertion written to recover information in view of explicit standards. The consequence of composing a SQL query will be a bunch of information that meet the models you framed in your query. You will frequently depend on SQL keywords to assist you with characterizing the standards in your query.

A query will bring information back.



10 Essential SQL Commands and Function every Programmer should learn

Here are the most essential SQL commands and functions I believe every programmer should learn. This will help you to work in any relational database like Oracle, MySQL, Microsoft SQL Server, PostgreSQL, and others. 

Here are  the top most usually utilized SQL questions:

10 SQL Commands and Functions Every Developer should learn



1. Creating the table (CREATE TABLE)

To create a spic and span, void table, you will utilize the SQL watchword "CREATE TABLE." for instance, we should take a gander at the accompanying SQL query:


CREATE TABLE student (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);


This query will create a table called "student" with zero lines and three columns. This table will store records of students. It will have student ID, student name, and student age. Its construction will be this way:


ID (Primary key)         Name     Age


Here, the column ID, set as a primary key, implies that the ID column can have just one of a kind qualities, and cells in this column can't be left clear.

While composing this kind of query, you should characterize the person type after the title of every column. In the model over, the Name column, characterized in the query as "TEXT," implies that it can incorporate characters. The Age column, characterized in the query as "Whole number," implies it can hold numbers (not letters or special characters).


2. Inserting data into table (INSERT INTO)

You can undoubtedly embed lines of information, or records, into a new or existing table. To do this, you will utilize the INSERT INTO order, with the SQL watchword, VALUES. This SQL query contains two sections: the initial segment recognizes the columns that will have information in them, and the subsequent part characterizes the information to place into those columns.

You will initially type INSERT INTO, trailed by the name of the table you are working with. Then, in brackets, list the columns that will contain information in this new line.

Then, you will type VALUES and, in brackets, characterize the specific qualities that will be filled in the particular columns, in the very request that you characterized the column names. These qualities ought to be composed inside single quotes, isolated by commas.

Suppose you need to add another student named Alan, matured 28, to your student table.

Basically type the accompanying SQL query:

INSERT INTO student (id, name, age) VALUES (1, Alan, 28);


In the wake of terminating the above query, our table will seem to be this:



We can fire all the more such queries to fill records in our table. For instance:

Insert into student (id, name, age) values (2, Amy, 26);
Insert into student (id, name, age) values (3, Bob, 27);
Insert into student (id, name, age) values (4, Chris, 28);
Insert into student (id, name, age) values (5, Dan, 26);


After executing above queries, table looks like this:



3. Viewing all records (SELECT)

The SELECT assertion addresses one of the least difficult and most often utilized in SQL. It permits you to see each of the records from a table that you indicate.


To utilize the SELECT assertion, essentially type:

SELECT * FROM student;


Utilize the SQL watchword FROM to characterize the name of the table from which you might want to see records.


The consequence of this query will be a showcase of all columns present in the table you named:






4. Ordering records of table (ORDERBY)

You utilize the ORDER BY proviso with your SELECT query to organize the showed brings about a specific request.


For instance, on the off chance that you type the accompanying:

SELECT * FROM student ORDER BY age;


You will get the accompanying return:






As you can see over, the result is organized in expanding request by age. The ORDER BY statement will naturally orchestrate the records in expanding request. To see your records showed in diminishing request, you can basically embed the SQL catchphrase DESC watchword into your query following the column name.


This would look as follows:
SELECT * FROM student DESC ORDER BY age;


Output:




5. Viewing only selected rows (SELECT COUNT)

On the off chance that a colossal number of lines exist in a table, and you don't believe every one of the records should fill your presentation screen, then, at that point, SQL gives a choice to see just chosen columns. You can utilize the SELECT COUNT statement to do this, as seen underneath:SELECT COUNT(1) FROM student;


Output:


You can likewise involve the MAX capability or MIN capability in your SELECT COUNT query. For instance, if you need to recover subtleties of students up to a most extreme age, you can fire the accompanying query:


SELECT id , name , MAX(age) FROM student;


Output:


6. Deleting records (DELETE)

To delete chosen columns from a table, fire the accompanying query:

DELETE FROM student WHERE name = 'alan';

This query will take a gander at the table called "Student" and will delete the whole line (or numerous columns) any place the Name column has the worth "alan."


For our situation, the consequence of this query will be the accompanying table:



7. Updating existing table data (UPDATE)

The UPDATE condition permits you to change data for records that as of now exist in your table. Assume you need to change the age of a student named 'Amy' in your table. You would fire this query:

UPDATE student SET age = 22 WHERE name = 'amy';


Note that assuming that the column referred to contains characters (as the Name column does), you should indicate the particular worth inside single quotes: 'amy.'


Subsequent to composing the query above, on the off chance that you, fire:

SELECT * FROM student;


Output:




As you can see over, Amy's age has been transformed from its unique worth (26) to 22.

Be cautious while utilizing the UPDATE or DELETE queries with the assistance of the WHERE provision. Assume that your Student table contains more than one student with the name 'Amy.' If you compose the UPDATE query over, the age of all students with the name 'Amy' will be refreshed to 22.

You ought to constantly utilize PRIMARY KEY in WHERE provision while refreshing or erasing. There will be a remarkable primary key for each column. Regardless of whether three records exist with the name Amy, they will each have an unmistakable primary key. Consequently, you can utilize Primary Key as opposed to Name in your query to refresh or delete data, guaranteeing that your query influences just a single record.

You likewise need to observe the information type for a given column while changing the information in it. A numeric column can contain numbers, while a text column can contain text. Assuming you attempted to utilize the UPDATE articulation to put age = 'Amy' in the Age column, SQL can not finish the solicitation and will get back with an exemption.


8. Viewing records without knowing exact details (LIKE)

In actuality, when you cooperate with a data set, you likely may not have a clue about all of the specific column values. For instance, as an information administrator in school, you might know about a student named Kellie in your school from hearing different educators discussing her. Maybe you need to track down the whole records for Kellie, yet you don't have any idea how she spells her name — "Kellie," "Kelly," or "Kelli."


For this situation, you can involve the LIKE administrator in SQL to assist you with finding a column of information when you just know a portion of the characters included.


You might fire the accompanying query:

SELECT * FROM student WHERE name LIKE 'kell%';


The result of this query will show all of you lines of students where the information in the Name column starts with "Kell" and closes with any letters. This can assist you with finding information when you have fractional data, as opposed to sure of the specific subtleties.


9. Using conditions (WHERE)

To grasp the necessity of utilizing the SQL watchword, WHERE, let us first supplement another line in our table — we will embed a 6th column with a student named Dan (utilizing an INSERT INTO query, as portrayed previously).


Our table presently becomes as follows:



Presently, in the event that we might want to see the new student Dan's records, we might fire our query as:

SELECT * FROM student WHERE name = 'dan';


The result will then be as per the following:



As you can see above, you couldn't get a one of a kind record by simply involving the Name esteem in the WHERE proviso. For this situation, there exist different records with similar information in the Name column.


10. Getting structure of table (DESCRIBE)

With such an excess of information that associations need to make due, you can't be anticipated to retain the columns and configuration of each and every table in your data set. While working with heaps of information and survey it in pieces, you can undoubtedly fail to remember the number of columns a particular table that has, which column addresses the primary key, or which character types have a place in every column.


Luckily, the planners of different SQL engines have given simple methods for minding the design of a specific table, so you can invigorate yourself on the arrangement. Each SQL engine has its own order for this activity, so you'll have to realize which one applies to your SQL engine.


For instance, taking the table we characterized as 'student,' to mind this table's construction in SQLite3, you would utilize the accompanying order:

.schema student;


While in PostgreSQL, the order will be:

\d student


MySQL utilizes the accompanying order:

describe student;


These will give you a similar outcome — showing you the construction of the 'student' table, distinguishing the number of columns it that has, their names and organizations, alongside which column is utilized as the primary key.


That's all about the essential SQL commands every programmer or developer should learn. We have covered some of the most important SQL queries which are must to start on before advancing to complex queries.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.