SQL Queries Simplified
Here is the previous arcticle by Kanchana.
Have you ever not been able to describe a picture in words? Then read on..
Google has developed a software that can read an image and accurately describe it in a few words the first time it sees it. It is a machine learning system. It used image recognition and natural language processing software that is Artificial Intelligence to help search images faster and describe images. The description by the software was very close to that of the description by humans. “A picture may be worth a thousand words” is exactly what the scientists who achieved this wrote on
Google Research blog. This software can be used in places where images are slow to load, help the visually impaired in reading images and use Google more for searching images.
So we hope it evolves to help us decode abstract expressions!!
Now moving on to SQL. What is a query?
A query, literally means requesting information. In database terms it means requesting information from the database. A query to the database is written in a specific language /format that the database understands. There is specific syntax for the language.
Structured Query Language (SQL) is the standard computer language to perform updation and querying on the database, according to ANSI. It is also pronounced as SEQUEL. It derives its origins from the mathematical set theory. SQL was developed based on a research paper submitted by Dr.E.F.Codd. His paper “A Relational Model of Data for Large Shared Data Banks” in the Association of Computer Machinery journal was the foundation for relational database management systems.
SQL has various flavors like PLSQL, T-SQl which are used by various vendors. They are all ANSI compliant.PL SQL is Oracle’s variant.PL SQL is programmatic that is procedural where as SQL is not.
So is T SQL for sql server. The SQL statements are that which constitute PL SQL. They actually perform the job of working with the data where as PL SQL helps to code the way to perform that is it uses loops, triggers, procedures etc in the making of a block of code that will help perform in a procedural manner. So it is important to know SQL first.
A simple and common SQL command is to fetch data from the tables of a database.
SELECT * from CUSTOMERS;
SELECT Cust_Name,Email from CUSTOMERS;
SELECT Cust_Name as ”Customers Name”, Email as “ Email Address” from CUSTOMERS;
It could also include math functions like sum,avg and joins and look slightly complex as below.
UNION,INTERSECT AND EXCEPT operators are commands that derive their names from mathematical set theory. Their functionality too is so.The ANSI standard for SQL requires that the tables on which these operators are used have matching columns and with the same datatype.
The below is an example of INNER JOIN
SELECT c1.customer_name,c1.Phone_no,a1.account_no,a1.balance
From c1 CUSTOMER,a1 ACCOUNTS
INNER JOIN CUSTOMERS
on c1.customer_no = a1.customer_no ;
Customer_name | Phone | Account_no | Balance |
Travolta | 4438474 | A023 | 78890 |
Waheeda | 3849948 | A111 | 67890 |
Kishore | 4838748 | A232 |
This join only lists those records whose customer number is in CUSTOMER and in ACCOUNTS table.
Drawing it in set relations it is that portion that is at the intersection of both the circles that is listed as a result of this INNER JOIN statement where each circle represents one table.
Lets revisit the table designs..
CUSTOMERS
Customer_no |
Cust_name |
Mid_name |
Last_name |
Street name |
City |
Pincode |
Accounts
Acc_no |
Acc_type |
Credit_limit |
Balance |
Description |
Transaction
Transaction_no |
Customer_no |
Amount |
Desc |
Time_Stamp |
Balance |
The CUST_AC table (has just two rows)
Customer_no |
Acc_no |
SQL statements using INNER JOIN can be written on multiple tables.
We will see them next.