SQL Server is a relational database management system, or RDBMS, developed and marketed by Microsoft. The interface helps to communicate the system’s database using queries.SQL queries are SQL’s mechanism to communicate with the database to result in us the desired output. A database is an organized collection of structured information, or data, typically stored electronically in a computer system.
The Insert Query
The Insert command is used to insert records into a table. The SQL ‘INSERT INTO’ declaration is used to include new database rows in the database table.
INSERT INTO TABLE_NAME (column1, column2, column3,…columnN)] VALUES (value1, value2, value3,…valueN);
Here, column1, column2,…columnN are the names of the columns in the table into which you want to insert data.
If you add value for all columns of the table you may not have to specify the column(s) name in the SQL query. Nonetheless, notice that the order of the values is identical to that of the table columns. It will be the following SQL INSERT INTO syntax:
INSERT INTO TABLE_NAME (Column1, Column2, ………., ColumnN) VALUES (value1,value2,value3,…valueN);
As per the syntax of INSERT INTO syntax, we specify the name of the table “INSERT INTO” keyword, followed the order of columns, in which you want the values that need to be inserted. Post the “VALUES’ keyword enter the column values in order of the columns specified earlier.
INSERT INTO student (id, name, age) VALUES (‘1’, ‘Kevin’, ‘Tirana’, 23);
After firing this query, our table will look like:
We can fire more such queries to fill records in our table:
Insert into student (id, name, address, age) values (‘2’, ‘Andy’, ‘Belgium ‘20’);
Insert into student (id, name, address, age) values (‘3’, ‘Roy’, ‘Chicago’ ‘58’);
The Select Query
Select query helps us get records back from a table. The result tables are called result-sets.
Syntax: The basic syntax of the SELECT statement is as follows:
SELECT column1, column2, columnN FROM table_name;
Here, column1, column2…are the fields of a table whose values you want to fetch. If you want to fetch all the fields available in the field, then you can use the following
SELECT * FROM table_name;
SELECT * FROM student;
The result of this query will be a display of all rows present in the table.
We may also use ORDER BY to organize the presented outcome in a similar order in our chosen sentence. For example,
SELECT * FROM student ORDER BY age;
Delete Records Query
To delete records from a table just use this query below it’s simple and easy to understand.
Syntax: DELETE FROM student WHERE [condition];
DELETE FROM student WHERE name = ‘Roy’;
This query will delete the entire row, or more than one row, from table ‘student’ where ‘name’ column has value ‘Rohit’.
In our case, the result of this query will look like the following table:
The Update Query
Changing data in existing records in a table is done by using the update query.
Syntax: The basic syntax of UPDATE query with WHERE clause is as follows:
UPDATE table_nameSET column1 = value1, column2 = value2…., columnN = valueNWHERE [condition];
Assume that we want to alter a student’s age in our table called ‘Rohit.’ The following query will be used:
UPDATE student SET age = 70 WHERE name = ‘Roy’;
You may have found that while the values are characters, we define the term in single quotes. That’s an obligation.
Now if we fire:
SELECT * FROM student;
We will get the following table as output:
Please pay attention to UPDATE or DELETE queries using the WHERE clause. Suppose there is more than one student called ‘Roy’ in our ‘class’ list. The age of all students called ‘Roy’ is modified to 70 in this case. Therefore, the PRIMARY KEY should always be included in the WHERE clause during the modification or deletion.
View only selected records
When we do not want all records to fill our display screen, SQL offers the choice to show only selected rows while there are a number of rows in a database. Count is good for recording record numbers.
Syntax: SELECT COUNT(1) FROM table_name;
SELECT COUNT(1) FROM student;
The output of this query will be: