This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
📚 preface
SQL has a history of more than 40 years and has been used almost everywhere since then. Every payment we consume, every piece of user information we collect, every message we send is stored in a database or a product associated with it, and SQL is the language used to manipulate the database!
SQL is almost a necessary skill for the production and research positions of Internet companies now. If you don’t know SQL, you may not be able to do anything. Think of SQL as a tool that will help you do your job and create value.
There is a SQL quiz at the end of this article! See how you score?
👉 🏻Click my jump to SQL quiz!
Introduce 🌴 SQL
🌼 What is SQL
SQL is a standard computer language for accessing and processing databases.
- SQL stands for structured Query Language
- SQL gives us the ability to access the database
- SQL is an ANSI standard computer language
SQL works with database programs, such as MS Access, DB2, Informix, MS SQL Server, Oracle, Sybase, and other database systems. But because of the variety of databases available, there are many different versions of the SQL language that must support major keywords (such as SELECT, UPDATE, DELETE, INSERT, WHERE, and so on) in a similar way in order to be ANSI compliant. These are the BASICS of SQL that we are going to learn.
🌀 Type of SQL
SQL can be divided into two parts: data Manipulation Language (DML) and data Definition Language (DDL).
- Data Query Language (DQL)
- Data Manipulation Language (DML)
🌵 Learn about SQL
SQL is an ANSI standard computer language for accessing and manipulating database systems. SQL statements are used to retrieve and update data from a database.
- SQL performs queries for the database
- SQL retrieves data from a database
- SQL inserts new records into the database
- SQL updates data in a database
- SQL deletes records from the database
- SQL to create a new database
- SQL creates new tables in a database
- SQL creates stored procedures in a database
- SQL creates views in the database
- SQL allows you to set permissions on tables, stored procedures, and views
🍄 What is a database
As the name implies, you can think of a database as a container for storing data.
For example, everyone has a refrigerator at home. What is the refrigerator for? A refrigerator is a place where food is kept.Similarly, a database is a place where data is stored. Because of the database, we can look up the data directly. For example, if you use Yu ‘ebao to check your account income every day, the data will be read from the database and given to you.
The most common type of database is a relational database management system (RDBMS) :
RDBMS is the foundation of SQL, as well as all modern database systems, such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. RDBMS
Data in is stored in database objects called tables.table
Is a collection of related data items, consisting of columns and rows.
Since this article focuses on THE basics of SQL, there is no explanation of databases, just a general understanding of them. Let’s get straight to SQL!
🐥 SQL basic language learning
Before we get into the basics of SQL, let’s talk about what a table is.
A database usually contains one or more tables. Each table is identified by a name (for example, “customer” or “order”). Tables contain records (rows) with data.
Here is an example of a table named Persons:
Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Adams | John | Oxford Street | London |
2 | Bush | George | Fifth Avenue | New York |
3 | Carter | Thomas | Changan Street | Beijing |
The table above contains three records (each for a person) and five columns (Id, last name, first name, address, and city).
How do you create a table to query?
🐤 CREATE TABLE – Creates a TABLE
The CREATE TABLE statement is used to CREATE tables in a database.
Grammar:
CREATE TABLETable name (column name1Data type, column name2Data type, column name3Data type,....) ;Copy the code
The data type (datA_type) specifies which data types a column can hold. The following table contains the most commonly used data types in SQL:
The data type | describe |
---|---|
integer(size),int(size),smallint(size),tinyint(size) | Holds only integers, specifying the largest number of digits in parentheses |
decimal(size,d),numeric(size,d) | Contains numbers with decimals, “size” specifies the largest number of digits, and “D” specifies the largest number of digits to the right of the decimal point |
char(size) | Contains a string of fixed length (including letters, digits, and special characters), specifying the length of the string in parentheses |
varchar(size) | Contains a string of variable length (including letters, digits, and special characters), specifying the maximum length of the string in parentheses |
date(yyyymmdd) | Hold the date |
Example:
Create table “Persons”;
This table contains 5 columns named “Id_P”, “LastName”, “FirstName”, “Address”, and “City” :
CREATE TABLE Persons
(
Id_P int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255));Copy the code
The data type of the Id_P column is int and contains integers. The data type for the remaining four columns is VARCHAR, with a maximum length of 255 characters.
An empty “Persons” table looks like this:
You can use the INSERT INTO statement to write data to an empty table.
🐑 INSERT – Inserts data
The INSERT INTO statement is used to INSERT new rows INTO the table.
Grammar:
INSERT INTOThe name of the tableVALUES(value1And the value2,...). ;Copy the code
We can also specify the column to which we want to insert data:
INSERT INTOTable_name (column1Column,2,...).VALUES(value1And the value2,...). ;Copy the code
Example:
Select * from Persons where Persons = 1;
Insert a new row
INSERT INTO Persons VALUES (1.'Gates'.'Bill'.'Xuanwumen 10'.'Beijing');
Copy the code
Insert data into the specified column
INSERT INTO Persons (LastName, Address) VALUES ('Wilson'.'Champs-Elysees');
Copy the code
After the insertion is successful, the following data is displayed:
After this data is inserted, it is through the SELECT statement to query out, don’t worry about it immediately!
🐼 SELECT – Query data
The SELECT statement is used to SELECT data from a table, and the results are stored in a result table (called a result set).
Grammar:
SELECT * FROMTable name;Copy the code
We can also specify the column for which we want to query data:
SELECTColumn nameFROMTable name;Copy the code
📢 Note: SQL statements are case-insensitive. SELECT is equivalent to SELECT.
Example:
SQL > SELECT * from
SELECT * FROM Persons;
Copy the code
📢 Note: The asterisk (*) is a shortcut to select all columns.
To obtain the contents of a column named “LastName” and “FirstName” (from a database table named “Persons”), use a SELECT statement like this:
SELECT LastName,FirstName FROM Persons;
Copy the code
🐫 DISTINCT – Removes duplicate values
If there are multiple rows of duplicate data in a table, how do you redisplay it? You can see DISTINCT.
Grammar:
SELECT DISTINCTColumn nameFROMTable name;Copy the code
Example:
To SELECT all values from the “LASTNAME” column, we need to use the SELECT statement:
SELECT LASTNAME FROM Persons;
Copy the code
It can be seen that Wilson is listed several times in the result set.
To SELECT only a DISTINCT value from the “LASTNAME” column, we need to use the SELECT DISTINCT statement:
SELECT DISTINCT LASTNAME FROM Persons;
Copy the code
With the above query, only one Wilson column is displayed in the result set, and the duplicate columns have obviously been removed.
🐸 WHERE – Conditional filtering
If you need to SELECT the specified data from the table, add the WHERE clause to the SELECT statement.
Grammar:
SELECTColumn nameFROMThe name of the tableWHEREColumn operator values;Copy the code
The following operators can be used in the WHERE clause:
The operator | describe |
---|---|
= | Is equal to the |
<> | Is not equal to |
> | Is greater than |
< | Less than |
> = | Greater than or equal to |
< = | Less than or equal to |
BETWEEN | Within a certain range |
LIKE | Search for a pattern |
📢 Note: In some versions of SQL, the <> operator can be written as! =.
Example:
If we only want to SELECT people who live in the city “Beijing”, we need to add a WHERE clause to the SELECT statement:
SELECT * FROM Persons WHERE City='Beijing';
Copy the code
📢 Note: SQL uses single quotes to surround text values (most database systems also accept double quotes). For numeric values, do not use quotation marks.
🐹 AND & OR – operator
AND AND OR combine two OR more conditions in the WHERE substatement.
- If both the first condition AND the second condition are true, the AND operator displays a record.
- The OR operator displays a record if only one of the first and second conditions is true.
Grammar:
Example of the AND operator:
SELECT * FROMThe name of the tableWHEREColumn operator valueANDColumn operator values;Copy the code
Example of the OR operator:
SELECT * FROMThe name of the tableWHEREColumn operator valueORColumn operator values;Copy the code
Example:
Select * from Persons where (select * from Persons where (select * from Persons))
INSERT INTO Persons VALUES (2.'Adams'.'John'.'Oxford Street'.'London');
INSERT INTO Persons VALUES (3.'Bush'.'George'.'Fifth Avenue'.'New York');
INSERT INTO Persons VALUES (4.'Carter'.'Thomas'.'Changan Street'.'Beijing');
INSERT INTO Persons VALUES (5.'Carter'.'William'.'Xuanwumen 10'.'Beijing');
SELECT * FROM Persons;
Copy the code
Example of the AND operator:
Use AND to display all people with the last name “Carter” AND the first name “Thomas” :
SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter';
Copy the code
Example of the OR operator:
Use OR to display all people with the last name “Carter” OR “Thomas” :
SELECT * FROM Persons WHERE firstname='Thomas' OR lastname='Carter';
Copy the code
Combining the AND AND OR operators:
We can also combine AND AND OR (using parentheses to form complex expressions) :
SELECT * FROM Persons WHERE (FirstName='Thomas' OR FirstName='William') AND LastName='Carter';
Copy the code
🐰 ORDER BY – Sort
The ORDER BY statement is used to sort the result set according to the specified column. BY default, the records are sorted in ascending ORDER. If you want to sort the records in descending ORDER, you can use the DESC keyword.
Grammar:
SELECT * FROMThe name of the tableORDER BY 列1Column,2 DESC;
Copy the code
The default sort is ASC ascending and DESC descending.
Example:
Display LASTNAME names in alphabetical order:
SELECT * FROM Persons ORDER BY LASTNAME;
Copy the code
NULL values are sorted by default after rows with values.
Display ID_P numerically and LASTNAME alphabetically:
SELECT * FROM Persons ORDER BY ID_P,LASTNAME;
Copy the code
Display ID_P in numeric descending order:
SELECT * FROM Persons ORDER BY ID_P DESC;
Copy the code
📢 Note: When there are identical values in the first column, the second column is sorted in ascending order. The same is true if some of the values in the first column are null.
🐱 UPDATE – Updates data
The Update statement is used to modify data in a table.
Grammar:
The UPDATE table nameSETColumn name=The new valueWHEREColumn name=A certain value;Copy the code
Example:
Update a column in a row:
Select * from Persons where LASTNAME = “Wilson” and LASTNAME = “FIRSTNAME”;
UPDATE Persons SET FirstName = 'Fred' WHERE LastName = 'Wilson';
Copy the code
Update several columns in a row:
UPDATE Persons SET ID_P = 6,city= 'London' WHERE LastName = 'Wilson';
Copy the code
🐨 DELETE – Deletes data
The DELETE statement is used to DELETE rows from a table.
Grammar:
DELETE FROMThe name of the tableWHEREColumn name=Value;Copy the code
Example:
Delete a line:
Select * from Persons where LastName = “Fred Wilson”;
DELETE FROM Persons WHERE LastName = 'Wilson';
Copy the code
Delete all rows:
You can delete all rows without deleting the table. This means that the structure, attributes, and indexes of the table are complete:
DELETE FROM table_name;
Copy the code
🐵 TRUNCATE TABLE – Clears TABLE data
What if we only need to remove the data in the table, but not the table itself?
You can use the TRUNCATE TABLE command (only delete data from the TABLE) :
Grammar:
TRUNCATE TABLETable name;Copy the code
Example:
Drop table “Persons”;
TRUNCATE TABLE persons;
Copy the code
🐯 DROP TABLE – DROP a TABLE
The DROP TABLE statement is used to DROP a TABLE (its structure, attributes, and indexes are also dropped).
Grammar:
DROP TABLETable name;Copy the code
Example:
Drop table “Persons”;
drop table persons;
Copy the code
Mysql > alter table select * from persons; alter table select * from persons; alter table select * from persons;
🚀 SQL Advanced language learning
🚢 LIKE – Find similar values
The LIKE operator is used to search for the specified pattern in a column in the WHERE clause.
Grammar:
SELECTThe column name/(*) FROMThe name of the tableWHEREColumn nameLIKEValue;Copy the code
Example:
Select * from Persons where id = 1;
INSERT INTO Persons VALUES (1.'Gates'.'Bill'.'Xuanwumen 10'.'Beijing');
INSERT INTO Persons VALUES (2.'Adams'.'John'.'Oxford Street'.'London');
INSERT INTO Persons VALUES (3.'Bush'.'George'.'Fifth Avenue'.'New York');
INSERT INTO Persons VALUES (4.'Carter'.'Thomas'.'Changan Street'.'Beijing');
INSERT INTO Persons VALUES (5.'Carter'.'William'.'Xuanwumen 10'.'Beijing');
select * from persons;
Copy the code
(1) select Persons from Persons who live in cities beginning with “N” :
SELECT * FROM Persons WHERE City LIKE 'N%';
Copy the code
2. Select Persons from the table “Persons” who live in cities ending in “g” :
SELECT * FROM Persons WHERE City LIKE '%g';
Copy the code
Select Persons (lon) from Persons (lon)
SELECT * FROM Persons WHERE City LIKE '%on%';
Copy the code
Select Persons from Persons who live in cities that do NOT contain lon:
SELECT * FROM Persons WHERE City NOT LIKE '%on%';
Copy the code
📢 Note: “%” can be used to define wildcards (missing letters in the schema).
🚤 IN – Locks multiple values
The IN operator allows us to specify multiple values IN the WHERE clause.
Grammar:
SELECTThe column name/(*) FROMThe name of the tableWHEREColumn nameIN(value1And the value2And the value3);
Copy the code
Example:
Select * from Persons where the last names are Adams and Carter:
SELECT * FROM Persons WHERE LastName IN ('Adams'.'Carter');
Copy the code
⛵️ BETWEEN – Select interval data
The operator BETWEEN… AND takes the data range between the two values. These values can be numeric, text, or dates.
Grammar:
SELECTThe column name/(*) FROMThe name of the tableWHEREColumn nameBETWEEN 值1 AND 值2;
Copy the code
Example:
Select * from ‘Adams’; select * from’ Carter ‘; select * from ‘Adams’;
SELECT * FROM Persons WHERE LastName BETWEEN 'Adams' AND 'Carter';
Copy the code
Select * from ‘NOT’ where ‘NOT’ = ‘NOT’
SELECT * FROM Persons WHERE LastName NOT BETWEEN 'Adams' AND 'Carter';
Copy the code
📢 Note: Different databases BETWEEN… The AND operator is handled differently.
Some databases list people between “Adams” and “Carter”, but not “Adams” and “Carter”; Some databases will list people between “Adams” and “Carter” and include “Adams” and “Carter”; Other databases list people between “Adams” and “Carter, “including “Adams” but not “Carter.”
So, please check how your database handles BETWEEN…. AND operator!
🚂 AS – Alias
Using SQL, you can specify aliases for column and table names. Aliases make queries easier to read and write.
Grammar:
The table alias:
SELECTColumn name/(*) FROMThe name of the tableASThe alias;Copy the code
Column alias:
SELECTColumn nameasThe aliasFROMTable name;Copy the code
Example:
Aliases with table names:
SELECT p.LastName, p.FirstName
FROM Persons p
WHERE p.LastName='Adams' AND p.FirstName='John';
Copy the code
Alias with column names:
SELECT LastName "Family", FirstName "Name" FROM Persons;
Copy the code
📢 Note: In practice, this AS can be omitted, but column aliases need a “”.
🚁 JOIN – Multiple table association
JOIN is used to query data from two or more tables based on the relationship between the columns in those tables.
Sometimes we need to get results from two or more tables in order to get complete results. We need to perform a join.
Tables in a database can be linked to each other by keys. A Primary Key is a column in which each row has a unique value. In a table, each primary key has a unique value. The goal is to cross-bind data between tables without repeating all the data in each table.
The “Id_P” column is the primary key in Persons. This means that no two rows can have the same Id_P. Id_P can distinguish between two people even if they have exactly the same name.
❤️ To continue the experiment below, we need to create another table: Orders.
create table orders (id_o number,orderno number,id_p number);
insert into orders values(1.11111.1);
insert into orders values(2.22222.2);
insert into orders values(3.33333.3);
insert into orders values(4.44444.4);
insert into orders values(6.66666.6);
select * from orders;
Copy the code
As shown, the “Id_O” column is the primary key in the Orders table, and the “Id_P” column in the “Orders” table is used to refer to Persons without using their names.
select * from persons p,orders o where p.id_p=o.id_p;
Copy the code
As you can see, the “Id_P” column connects the two tables above.
Grammar:
selectThe column namefromTable AINNER|LEFT|RIGHT|FULL JOINTable BONTable A primary key column=Table B foreign key column;Copy the code
Different SQL joins:
The following lists the types of joins you can use and the differences between them.
- JOIN: Returns a row if there is at least one match in the table
- INNER JOIN: An INNER JOIN that returns the matched rows in two tables
- LEFT JOIN: Returns all rows from the LEFT table even if there is no match in the right table
- RIGHT JOIN: Returns all rows from the RIGHT table even if there is no match in the left table
- FULL JOIN: Returns rows as long as there is a match in one of the tables
Example:
If we want to list all orders, we can use the following SELECT statement:
SELECT p.LastName, p.FirstName, o.OrderNo
FROM Persons p
INNER JOIN Orders o
ON p.Id_P = o.Id_P
ORDER BY p.LastName DESC;
Copy the code
🚜 UNION – Merge result sets
The UNION operator is used to combine the result sets of two or more SELECT statements.
The UNION grammar:
SELECTThe column nameFROMTable AUNION
SELECTThe column nameFROMTable B;Copy the code
📢 Note: the UNION operator defaults to a different value. If the query results need to display duplicate values, use UNION ALL.
UNION ALL syntax:
SELECTThe column nameFROMTable AUNION ALL
SELECTThe column nameFROMTable B;Copy the code
In addition, the column name in the UNION result set is always equal to the column name in the first SELECT statement in the UNION.
Create the Person_b table for the experiment:
CREATE TABLE Persons_b
(
Id_P int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255));INSERT INTO Persons_b VALUES (1.'Bill'.'Gates'.'Xuanwumen 10'.'Londo');
INSERT INTO Persons_b VALUES (2.'John'.'Adams'.'Oxford Street'.'nBeijing');
INSERT INTO Persons_b VALUES (3.'George'.'Bush'.'Fifth Avenue'.'Beijing');
INSERT INTO Persons_b VALUES (4.'Thomas'.'Carter'.'Changan Street'.'New York');
INSERT INTO Persons_b VALUES (5.'William'.'Carter'.'Xuanwumen 10'.'Beijing');
select * from persons_b;
Copy the code
Example:
Using the UNION command:
List the different persons in persons and persons_b:
select * from persons
UNION
select * from persons_b;
Copy the code
📢 Note: SELECT statements within the UNION must have the same number of columns. Columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.
🚌 NOT NULL – NOT NULL
The NOT NULL constraint enforces columns NOT to accept NULL values.
The NOT NULL constraint forces a field to always contain a value. This means that you cannot insert a new record or update a record without adding a value to the field.
Grammar:
CREATE TABLETable (columnint NOT NULL
);
Copy the code
As above, create a table and set column values that cannot be null.
Example:
create table lucifer (id number not null);
insert into lucifer values (NULL);
Copy the code
📢 Error: orA-01400 cannot be inserted if NULL is inserted.
⭐️ NOT NULL can also be used to query conditions:
select * from persons where FirstName is not null;
Copy the code
Similarly, NULL also works:
select * from persons where FirstName is null;
Copy the code
Interested friends, you can try it yourself!
🚐 VIEW – VIEW
In SQL, a view is a visual table based on the result set of an SQL statement.
A view contains rows and columns, just like a real table. Fields in a view are fields in real tables from one or more databases. We can add SQL functions, WHERE, and JOIN statements to the view, and we can submit data as if it came from a single table.
Grammar:
CREATE VIEWView nameAS
SELECTThe column nameFROMThe name of the tableWHEREQuery conditions;Copy the code
📢 Note: Views always display the most recent data. Each time the user queries the view, the database engine reconstructs the data by using SQL statements.
Example:
Select * from Persons who live in Beijing; select * from Persons who live in Beijing;
create view persons_beijing as
select * from persons where city='Beijing';
Copy the code
Query the above view:
If you need to update columns OR other information in a VIEW without deleting them, use the CREATE OR REPLACE VIEW option:
CREATE OR REPLACE VIEWView nameAS
SELECTThe column nameFROMThe name of the tableWHEREQuery conditions;Copy the code
Example:
Now we need to filter out the records where LASTNAME is Gates:
create or replace view persons_beijing as
select * from persons where lastname='Gates';
Copy the code
Dropping a view is as simple as dropping a table:
drop view persons_beijing;
Copy the code
❤️ this chapter to speak high-level language on the first so far, not a one-time introduction too much ~
🎯 Learn common SQL functions
SQL has many built-in functions that can be used for counting and calculation.
Function syntax:
SELECT function(column)FROMTable;Copy the code
❤️ Take a look at the common functions below!
🍔 AVG — Average
The AVG function returns the average value of a column of values. NULL values are not included in the calculation.
Grammar:
SELECT AVG(column name)FROMThe name of the table.Copy the code
Example:
Calculate the average value of the “OrderNo” field.
select avg(orderno) from orders;
Copy the code
Of course, it can also be used in query conditions, such as query for below-average records:
select * from orders where orderno < (select avg(orderno) from orders);
Copy the code
🍕 COUNT – Summary number of rows
The COUNT() function returns the number of rows matching the specified condition.
Grammar:
Count () can have different syntax:
- COUNT(*) : Returns the number of records in the table.
- COUNT(DISTINCT column name) : Returns the number of DISTINCT values for the specified column.
- COUNT(column name) : Returns the number of values for the specified column (NULL does not COUNT).
SELECT COUNT(*) FROMThe name of the table.SELECT COUNT(DISTINCTThe column name)FROMThe name of the table.SELECT COUNT(column name)FROMThe name of the table.Copy the code
Example:
COUNT (*) :
select count(*) from persons;
Copy the code
COUNT(DISTINCT column name) :
select count(distinct city) from persons;
Copy the code
COUNT(column name) :
select count(city) from persons;
Copy the code
🍘 MAX – Maximum value
The MAX function returns the maximum value in a column. NULL values are not included in the calculation.
Grammar:
SELECT MAX(column name)FROMThe name of the table.Copy the code
MIN and MAX can also be used for text columns to get the highest or lowest values in alphabetical order.
Example:
select max(orderno) from orders;
Copy the code
🍢 MIN – Minimum value
The MIN function returns the minimum value in a column. NULL values are not included in the calculation.
Grammar:
SELECT MIN(column name)FROMThe name of the table.Copy the code
Example:
select min(orderno) from orders;
Copy the code
🍰 SUM – SUM
The SUM function returns the total number of numeric columns.
Grammar:
SELECT SUM(column name)FROMThe name of the table.Copy the code
Example:
select sum(orderno) from orders;
Copy the code
🍪 GROUP BY – Groups
The GROUP BY statement is used to GROUP a result set BY one or more columns in conjunction with a summing function.
Grammar:
SELECTColumn name A, statistical function (Column name B)FROMThe name of the tableWHEREQuery conditionsGROUP BYThe column name A;Copy the code
Example:
Select * from Persons who live in Beijing and group by LASTNAME
select lastname,count(city) from persons
where city='Beijing'
group by lastname;
Copy the code
If GROUP BY is not added, error:
Ora-00937 is not a single grouping function error.
🍭 HAVING – sentence end connection
The reason for adding the HAVING clause in SQL is that the WHERE keyword cannot be used with the aggregate function.
Grammar:
SELECTColumn name A, statistical function (Column name B)FROM table_name
WHEREQuery conditionsGROUP BYColumn a.HAVINGStatistical function (column name B) query conditions;Copy the code
Example:
Select * from Persons where total number of Persons living in Beijing > 1;
select lastname,count(city) from persons
where city='Beijing'
group by lastname
having count(city) > 1;
Copy the code
🍷 UCASE/UPPER — uppercase
The UCASE/UPPER function converts the value of the field to uppercase.
Grammar:
select upper(column name)fromThe name of the table.Copy the code
Example:
Select the contents of the “LastName” and “FirstName” columns, then convert the “LastName” column to uppercase:
select upper(lastname),firstname from persons;
Copy the code
🍶 LCASE/LOWER – LOWER case
The LCASE/LOWER function converts the value of the field to lowercase.
Grammar:
select lower(column name)fromThe name of the table.Copy the code
Example:
Select the contents of the “LastName” and “FirstName” columns and convert the “LastName” column to lowercase:
select lower(lastname),firstname from persons;
Copy the code
👛 LEN/LENGTH – Obtains the LENGTH
The LEN/LENGTH function returns the LENGTH of the value in the text field.
Grammar:
selectLength (column name)fromThe name of the table.Copy the code
Example:
Get the value of LASTNAME
select length(lastname),lastname from persons;
Copy the code
🍗 ROUND – Value selection
The ROUND function is used to ROUND a numeric field to the specified decimal number.
Grammar:
selectRound (Column name, precision)fromThe name of the table.Copy the code
Example:
Reserved 2:
select round(1.1314.2) from dual;
select round(1.1351.2) from dual;
Copy the code
📢 note: the ROUND is ROUND!
Integer:
select round(1.1351.0) from dual;
select round(1.56.0) from dual;
Copy the code
🍞 NOW/SYSDATE – Current time
The NOW/SYSDATE functions return the current date and time.
Grammar:
select sysdate fromThe name of the table.Copy the code
Example:
Get the current time:
select sysdate from dual;
Copy the code
📢 Note: If you are using a Sql Server database, use the getDate () function to get the current date and time.
🍺 is at the end
If you have learned all of the above, you can take a quiz: SQL quiz to see how you have mastered it! ❤️ Quizzes will be scored:
Each question is worth one point. After you have completed all 20 questions, your quiz will be scored and the correct answers to the questions you got wrong will be provided. Green is the correct answer and red is the wrong answer.
☞ Begin the test now! Good luck.