{"id":67827,"date":"2020-10-12T12:31:52","date_gmt":"2020-10-12T12:31:52","guid":{"rendered":"https:\/\/www.fita.in\/?p=67827"},"modified":"2023-10-09T12:45:09","modified_gmt":"2023-10-09T12:45:09","slug":"sql-for-data-science-for-beginners","status":"publish","type":"post","link":"https:\/\/www.fita.in\/sql-for-data-science-for-beginners\/","title":{"rendered":"SQL For Data Science: For Beginners"},"content":{"rendered":"
Data has become the fuel for the organizations to strategize and formulate the entire models as per trending patterns and requirements. Databases are where these data reside, but working with them requires a programming language.<\/p>\r\n
SQL or Structured Query Language is a primary language for working with databases. It is used widely for data analysis or data science, it is a must to know the language for driving, reports, manipulation, or analysis of a dataset.<\/p>\r\n
With SQL you can update, add or delete records from the database.The top Relational Database Management Systems(RDBMS) such as PostgreSQL, MySql, SQLite, MariaDB, Oracle<\/a> are based on the SQL.<\/p>\r\n This blog is your way to go for analyzing simple datasets using SQL.<\/p>\r\nHere is what we will discuss step by step in this blog,\r\n -- syntax for creating a database<\/p>\r\n <\/p>\r\n CREATE DATABASE <databasename><\/p>\r\n USE <databasename><\/p>\r\n<\/code><\/pre>\r\n<\/div>\r\nHere the CREATE DATABASE command creates the database with the database name and the USE command will activate the database with the database name followed.\r\n -- Example for creating a database<\/p>\r\n <\/p>\r\n CREATE DATABASE FITA<\/p>\r\n USE FITA<\/p>\r\n<\/code><\/pre>\r\n<\/div>\r\nAfter creating the database let us now see how to create a table in the database.\r\n\r\nCreating Tables<\/strong>\r\n\r\nThe data in databases are stored in Tables with attributes and the type of data. A database can have more than one table. creating tables is just as creating a database\r\n -- syntax for creating a table<\/p>\r\n <\/p>\r\n CREATE TABLE <tablename><\/p>\r\n (<\/p>\r\n variable1 datatype 1,<\/p>\r\n variable 2 datatype 2,<\/p>\r\n variable n datatype n,<\/p>\r\n ..<\/p>\r\n );<\/p>\r\n<\/code><\/pre>\r\n<\/div>\r\n Here the CREATE TABLE will create a table with a table name. The variables here are the name of the attributes, which will be the columns of the table.SQL data can have numeric, character or string Date and Time, Binary, and Boolean data type. A semicolon indicates the termination.<\/p>\r\n\r\n -- Example for creating a table<\/p>\r\n <\/p>\r\n CREATE TABLE Students<\/p>\r\n (<\/p>\r\n roll number INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,<\/p>\r\n student_name TEXT,<\/p>\r\n marks INTEGER,<\/p>\r\n grade VARCHAR,<\/p>\r\n passed BOOLEAN<\/p>\r\n );<\/p>\r\n<\/code><\/pre>\r\n<\/div>\r\n Here the roll number is of type integer and the primary key is for identifying records with this unique value. There could be only one primary key for a record.<\/p>\r\n AUTOINCREMENT is used to increment the value automatically, that is you won\u2019t need to pass the value for the roll number and it will be automatically set.<\/p>\r\n After creating the tables in the database let us now see how to insert rows in the table.<\/p>\r\n\r\n -- Syntax for inserting a record<\/p>\r\n <\/p>\r\n INSERT INTO TABLE <tablename> VALUES<\/p>\r\n (<\/p>\r\n value 1,<\/p>\r\n value 2,<\/p>\r\n ..<\/p>\r\n value n<\/p>\r\n );<\/p>\r\n<\/code><\/pre>\r\n<\/div>\r\nBasic Of SQL<\/strong><\/h3>\r\nCreating Database<\/strong>\r\n\r\nA SQL Database is a godown for storing structured data. Here is how you would create a database in MySQL.\r\n
\r\n\r\n\r\n\r\nInsert into table<\/strong><\/h3>\r\nTo insert a record into the table you will need to specify all the values with the respective data type in the table.\r\n
\r\n\r\nINSERT INTO Students VALUES (NULL,\u201cAtufa\u201d,70,\u201dB\u201d,true);\r\n\r\nINSERT INTO Students VALUES (NULL,\u201cPuja\u201d,85,\u201dA\u201d,true);\r\n\r\nINSERT INTO Students VALUES (NULL,\u201cJane\u201d,50,\u201dB\u201d,true);\r\n\r\nINSERT INTO Students VALUES (NULL,\u201cRosy\u201d,45,\u201dC\u201d,true);\r\n\r\nINSERT INTO Students VALUES (NULL,\u201cHarry\u201d,20,\u201dF\u201d,false);\r\n<\/code><\/pre>\r\n<\/div>\r\nNotice that we didn’t pass the roll number because it has been set to AUTO_INCREMENT.\r\n\r\nNote that these commands need not to be written in capital letters on different lines necessarily, this is just to distinguish the SQL commands from the values.\r\n\r\nAfter inserting rows in the database let us now see how to retrieve data from the table in the database.\r\nRetrieving data<\/strong><\/h3>\r\n
-- Example for selecting all the records\r\n\r\n \r\n\r\nSELECT * FROM Students<\/code><\/pre>\r\n<\/div>\r\n\r\nHere the SELECT command is for retrieving data, and * for all the records.\r\n\r\nOutput for the above query<\/strong>\r\n\r\n\r\n
\r\n roll_number<\/strong><\/td>\r\n student_name<\/strong><\/td>\r\n marks<\/strong><\/td>\r\n grade<\/strong><\/td>\r\n passed<\/strong><\/td>\r\n<\/tr>\r\n \r\n 1<\/td>\r\n Atufa<\/td>\r\n 70<\/td>\r\n B<\/td>\r\n 1<\/td>\r\n<\/tr>\r\n \r\n 2<\/td>\r\n Puja<\/td>\r\n 85<\/td>\r\n A<\/td>\r\n 1<\/td>\r\n<\/tr>\r\n \r\n 3<\/td>\r\n Jane<\/td>\r\n 50<\/td>\r\n B<\/td>\r\n 1<\/td>\r\n<\/tr>\r\n \r\n 4<\/td>\r\n Rosy<\/td>\r\n 45<\/td>\r\n C<\/td>\r\n 1<\/td>\r\n<\/tr>\r\n \r\n 5<\/td>\r\n Harry<\/td>\r\n 20<\/td>\r\n F<\/td>\r\n 0<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n --\u00a0Example for selecting records for specific attributes\r\n\r\n \r\n\r\nSELECT student_name, passed FROM STUDENTS;<\/code><\/pre>\r\n<\/div>\r\nOutput for the above query<\/strong>\r\n\r\n\r\n
\r\n student_name<\/strong><\/td>\r\n passed<\/strong><\/td>\r\n<\/tr>\r\n \r\n Puja<\/td>\r\n 1<\/td>\r\n<\/tr>\r\n \r\n Jane<\/td>\r\n 1<\/td>\r\n<\/tr>\r\n \r\n Rosy<\/td>\r\n 1<\/td>\r\n<\/tr>\r\n \r\n Harry<\/td>\r\n 0<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\nHere the 0 represents false value or failed and 1 represents true value or passed.\r\n SELECT * FROM Students WHERE grade == \u2018B\u2019;<\/code><\/pre>\r\n<\/div>\r\nOutput for the above query<\/strong>\r\n\r\n\r\n
\r\n roll_number<\/strong><\/td>\r\n student_name<\/strong><\/td>\r\n marks<\/strong><\/td>\r\n grade<\/strong><\/td>\r\n passed<\/strong><\/td>\r\n<\/tr>\r\n \r\n 1<\/td>\r\n Atufa<\/td>\r\n 70<\/td>\r\n B<\/td>\r\n 1<\/td>\r\n<\/tr>\r\n \r\n 3<\/td>\r\n Jane<\/td>\r\n 50<\/td>\r\n B<\/td>\r\n 1<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\nOr to select specific records from specific columns with a condition using the WHERE command. Here is an exercise\r\n SELECT student_name FROM Students WHERE grade == \u2018A\u2019;<\/code><\/pre>\r\n<\/div>\r\nOutput for the above query<\/strong>\r\n