JavaScript

 Introduction To SQL


   What is SQL?

  • SQL stands for Structured Query Language
  • SQL is a standard language for accessing and manipulating databases.
  • SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987.
  • SQL keywords are NOT case sensitive.
    
    What Can SQL do?
  • SQL can execute queries against a database, retrieve data from a database, insert records in a database, update records in a database, records from a database,create new databases, new tables.
  • SQL can set permissions on tables, procedures, and views.
    For Example:
                  
     To build a web site that shows data from a database, you will need:
  • An RDBMS database program (i.e. MS Access, SQL Server, MySQL).
  • To use a server-side scripting language, like PHP or ASP.
  • To use SQL to get the data you want to work with.
  • To use HTML / CSS to style the page.
    What is RDBMS?

  • RDBMS stands for Relational Database Management System.
  • RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, Oracle, MySQL, and Microsoft Access.
  • The data in RDBMS is stored in database objects called tables.  
  • A table is a collection of related data entries and it consists of columns and rows.
  • Every table is broken up into smaller entities called fields.
  • Example : The fields in the  Student_info table consist of StudentID,StudentName, EmailId , Department and Grade. 
  • A field is a column in a table that is designed to maintain specific information  about every record in the table.
  • A record, also called a row, is each individual entry that exists in a table.
   For example, there are 4 records in the above Student_info table. A record is a horizontal entity in  a table.
   A column is a vertical entity in a table that contains all information associated with a specific field in a table.

 Database Tables:

 A database most often contains one or more tables. Each table is identified by a name (EX: Student_info). Tables contain records (rows) with data.



The table above contains four records (one for each student) and five columns (StudentID,StudentName, EmailId , Department and Percentage).

SQL Statements:

Most of the actions you need to perform on a database are done with SQL statements.

The following SQL statement selects all the records in the "student_info" table:

                                SELECT FROM student_info;

Semicolon after SQL Statements?

  • Some database systems require a semicolon at the end of each SQL statement.
  • Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.

Some of The Most Important SQL Commands

  • SELECT - extracts data from a database
  • UPDATE - updates data in a database
  • DELETE - deletes data from a database
  • INSERT INTO - inserts new data into a database
  • CREATE DATABASE - creates a new database
  • ALTER DATABASE - modifies a database
  • CREATE TABLE - creates a new table
  • ALTER TABLE - modifies a table
  • DROP TABLE - deletes a table
  • CREATE INDEX - creates an index (search key)
  • DROP INDEX - deletes an index

SQL (Database) Management Tools:

             MySQL, PostgreSQL, MySQL Workbench, SQLite, MS SQL, Oracle, and MongoDB. 

SELECT Column Example: 

The following SQL statement selects the "StudentName" and "percentage" columns from the "student_info" table:

Example: 

SELECT StudentName, Percentage FROM student_info;



 




SELECT  * Example:

The following SQL statement selects all the columns from the "student_info" table:

Example:

                     SELECT * FROM student_info;




SQL SELECT DISTINCT Statement:

The SELECT DISTINCT statement is used to return only distinct (different) values.

Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.

Syntax:

  SELECT DISTINCT column1, column2, ...

 FROM table_name;

Example: 
 
 SELECT DISTINCT studentname FROM student_info;

 Number of Record: 4



SQL statement lists the number of different (distinct) customer countries:

                
SELECT COUNT(DISTINCT percentage) FROM student_info;

RESULT:

Number of Records: 1

COUNT(DISTINCT Country)
4

SQL WHERE Clause:

The WHERE clause is used to filter records.

The WHERE clause is used to extract only those records that fulfill a specified condition.

Syntax: SELECT column1, column2, ...

               FROM table_name

               WHERE condition;

Example:          SELECT * FROM student_info
                        WHERE department='CSE';
 
Number of Record: 1


NOTE: SQL requires single quotes around text values (most database systems will also allow double quotes).However, numeric fields should not be enclosed in quotes.

Operators in The WHERE Clause :

The following operators can be used in the WHERE clause:














1 Comments

Post a Comment

Previous Post Next Post