SQL Tutorial: Introduction for beginners (Basic)

SQL tutorial – Introduction for beginners

1) What is SQL?

SQL (Structured Query Language) is a standard language for storing, managing and retrieving data in databases. SQL was developed at IBM by Donald Chamberlin and Raymond Boyce. and first released in 1991. SQL became a standard of the ANSI (American National Standards Institute) in 1986. In this post, you’ll learn how to communicate with the Databases using SQL.

Read:
How to create a WordPress blog using XAMPP
How to be a Programmer | Part 5 : Number Systems

2) Installation (MySQL)

Step 1 – Prerequisites

XAMPP – Apache (A) + MariaDB(M) + PHP(P) + Perl(P)

CtechF - How to install XAMPP - Screenshot

XAMPP is an Open source software. It’s a Stands of Cross-platform (X). we can create a local web server using XAMPP. It’s easy to use and it works with Windows, Linux, and Mac OS. You can use alternative software for XAMPP, such as WAMP, MAMP.

XAMPP Download link (121 MB) – https://www.apachefriends.org/index.html
WAMP download link (286 MB) – http://www.wampserver.com/en/#download-wrapper
MAMP download link (410 MB) – https://www.mamp.info/en/downloads/

Step 2 – How to install XAMPP.

  • Click on the downloaded XAMPP setup. (If you got any permission message or warning message click “OK” or “YES” button to continue) It will open the XAMPP setup wizard. Click the next button to continue.
CtechF - How to install XAMPP - Screenshot

Now it asked, what components we want to install and what’s not. We only need Apache and MySQL. (select following Components and click the next button to continue)

  • Server
    • Apache – Apache Server
    • MySQL – Database Server
  • Program Language
    • PHP – Hypertext Preprocessor
  • Program Languages
    • phpMyAdmin – Database Control panel
CtechF - How to install XAMPP - Screenshot
  • Choose installation location and click Next (Default: C:\xampp)  (If you got Windows Defender Firewall Permission request, Tick both checkboxes and click Allow access.)
  • Click the next button to continue Now it will install XAMPP (wait till the installation complete) After the installation complete. Click Finish.
  • It will open the XAMPP Control panel automatically.
  • This is a XAMPP Control Panel.
CtechF - How to install XAMPP - Screenshot
  • Now click Start Apache and MySQL (It will start Apache and MySQL servers)
CtechF - How to install XAMPP - Screenshot

Step 3 – Opening a MySQL in Terminal

Open XAMPP Control panel and click shell. It will open a terminal. then type “mysql” and press enter.

sql tutorial

3) SQL Tutorial

3.1)  SQL – Comments

--This is a comment

3.2)  SQL – CREATE Database

CREATE DATABASE DatabaseName;

3.3)  SQL – DROP Database (Delete Database)

DROP DATABASE DatabaseName;

3.4)  SQL – SELECT Database

USE DatabaseName;

3.5)  SQL – CREATE Table

Before creating a table, you need to select a database.

USE DatabaseName;

CREATE TABLE table_name(
   column1 datatype,
   column2 datatype,
   .....
   PRIMARY KEY(one or more columns)
);

3.6)  SQL – Insert New Records in a Table.

INSERT INTO table_name(column1, column2, ...) VALUES (value1, value2, ...);

3.7)  SQL – Select Data from a Database.

SELECT column1, column2, ... FROM table_name;

Select ALL

SELECT * FROM table_name;

3.8)  SQL – SELECT DISTINCT Statement

This statement is used to return only different values.

SELECT DISTINCT column1, column2, ... FROM table_name;

3.9)  SQL – WHERE Clause with AND, OR and NOT Syntax

AND Syntax

SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 ...;

OR Syntax

SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 ...;

NOT Syntax

SELECT column1, column2, ... FROM table_name WHERE NOT condition;

3.10)  SQL – ORDER BY Keyword

This keyword is used to sort the result in ascending (ASC) or descending (DESC) order.

SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC;

SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... DESC;

3.11)  SQL – NULL Values

NULL Values = No Value

SELECT column_names FROM table_name WHERE column_name IS NULL;

SELECT column_names FROM table_name WHERE column_name IS NOT NULL;

3.12)  SQL – UPDATE Statement

This Statement is used to modify the existing records in a table

UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

3.13)  SQL – DELETE Statement

This statement is used to delete records in a table.

DELETE FROM table_name WHERE condition;

3.14)  SQL – DELETE All Records

This statement is used to delete all records in a table.

DELETE FROM table_name;

3.15)  SQL – LIMIT Clause

SELECT column_name(s) FROM table_name WHERE condition LIMIT number;

3.16)  SQL –  COUNT(), AVG() and SUM()

Count

SELECT COUNT(column_name) FROM table_name WHERE condition;

Average

SELECT AVG(column_name) FROM table_name ToWHERE condition;

Total

SELECT SUM(column_name) FROM table_name WHERE condition;

3.17)  SQL – MIN() and MAX()

Min

SELECT MIN(column_name) FROM table_name WHERE condition;

Max

SELECT MAX(column_name) FROM table_name WHERE condition;

3.18)  SQL – LIKE Syntax

Wildcards

c% – Find values that start with “c”
%c – Find values that end with “c”
c%c – Find values that start with “c” and end with “c”
%ctechf% – Find values that have “ctechf” in any position.
_c% – Find values that have c in the second position.
[syl]% – Find all values that start with “s”, “y” or “l”
[c-f]% – Find all values that start with “c”, “d”, “e” or “f”
[!syl]% – Find all values that Not start with “s”, “y” or “l”

SELECT column1, column2, ... FROM table_name WHERE column_name LIKE pattern;

3.19)  SQL – BETWEEN Operator

This operator return values within a given range.

SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;

3.20)  SQL – NOT BETWEEN Operator

This operator return outside values within a given range.

SELECT column_name(s) FROM table_name WHERE column_name NOT BETWEEN value1 AND value2;

Hi Friends,

Hope this post (SQL tutorial) will help you to learn something, If you enjoy my work then please share my posts with your friends and anyone who might be interested in programming and don’t forget to subscribe my mailing list.

If you had any problem with this post, please do mention it in the comment section.

Like ctechf Facebook fan page : https://www.facebook.com/ctechf

Also Read :

Sources : 
https://pixabay.com/ (images)
ICT Books (Details)

Liked it? Take a second to support CtechF on Patreon!

Leave a Reply

33 Shares
Share
Tweet
Share
Pin
Share