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.
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.
A query will bring information back.
10 Essential SQL Commands and Functions
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.1. Creating the table (CREATE TABLE)
CREATE TABLE student (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);
ID (Primary key) Name Age
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.
2. Inserting data into table (INSERT INTO)
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.
INSERT INTO student (id, name, age) VALUES (‘1’, ‘Alan’, 28);
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’);
3. Viewing all records (SELECT)
SELECT * FROM student;
4. Ordering records of table (ORDERBY)
SELECT * FROM student ORDER BY age;
SELECT * FROM student DESC ORDER BY age;
5. Viewing only selected rows (SELECT COUNT)
SELECT id , name , MAX(age) FROM student;
6. Deleting records (DELETE)
DELETE FROM student WHERE name = 'alan';
7. Updating existing table data (UPDATE)
UPDATE student SET age = 22 WHERE name = 'amy';
SELECT * FROM student;
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.
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.
SELECT * FROM student WHERE name LIKE 'kell%';
9. Using conditions (WHERE)
SELECT * FROM student WHERE name = 'dan';
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;
\d student
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.
No comments:
Post a Comment