Pandas and SQL Flashcards

1
Q

<p>What happens if execute the following code?</p>

<p>Create newTable(col1 INT, col2 CHAR(25));</p>

A

<p>It will raise me error because you forgot to mention the keyword table .</p>

<p>Correct Syntax</p>

<p>Create table newTable(col1 INT, col2 CHAR(25);</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

<p>What is the difference between unqiue Key and primary key?</p>

A

<p>There may be many unique key columns in a table but there can be only ONE primary key column.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

<p>What happens when I assign a certain column as Unique key?</p>

A

<p>It makes sure that all the entries in that column are unique and has no duplicates</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

<p>Create a table with 1)Auto increment 2)uinique primary key 3)char , int and date-time columns .</p>

A

<p>CREATE TABLE Worker ( WORKER_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, FIRST_NAME CHAR(25), LAST_NAME CHAR(25), SALARY INT(15), JOINING_DATE DATETIME, DEPARTMENT CHAR(25) );</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

<p>What happens if I try to execute the following code?</p>

<p>CREATE TABLE NewTable(S.No INT PRIMARY KEY);</p>

A

<p>It will raise and error because '.' is not allowed for COLUMN name assingment. '_' Is allowed.So the appropriate word would be 'S_No'</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

<p>Insert values into table that has columns 'Column1'(INT) and 'Column2'(string)</p>

A

<p>INSERT INTO Table_Name (Column1, Column2) VALUES (001, 'Monika')</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

<p>What happens if I execute this ?</p>

<p>INSERT INTO TABLE newtable3(col1,col2,col3) VALUES (1,2,'Hello');</p>

A

<p>This will raise me an error because we DONT HAVE to specify keyword table in this case .</p>

<p>Correct Syntax:</p>

<p>INSERT INTO newtable3(col1,col2,col3) VALUES (1,2,'Hello');</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

<p>What is the differnce between create and Insert in terms of syntax?</p>

A

<p>In case of Create we will have to mention the keyword TABLE</p>

<p>But in case of Insert we SHOULDNT mention the keyword , or else it will give me syntax error</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

<p>Get all unique elements from duplicates in certain column of sql table</p>

A

<p>SELECT DISTINCT column1, column2, ...FROM table_name;</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

<p>Get all elements form a certain column in SQL table</p>

A

<p>Select column_name from table_name</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

<p>Get elements from certain column while specifying criteria SQL</p>

A

<p>SELECT column1, column2, ... FROM table_name WHERE column1>thresh_val;</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

<p>Get elements that DO NOT satisfy specific condition in said column</p>

A

<p>SELECT column1, column2, ...FROM table_nameWHERE NOT colum1="Sunny";</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

<p>Get elements with multiple condtions are applied at the same time</p>

A

<p>SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin';</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

<p>Get elments that is union of completely different search filters</p>

A

<p>SELECT * FROM CustomersWHERE Country='Germany' OR Country='Spain';</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

<p>Get elements in Ascending order</p>

A

<p>SELECT * FROM CustomersORDER BY Country ASC;</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

<p>Get elements in Descedning order</p>

A

<p>SELECT * FROM CustomersORDER BY Country DESC;</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

<p>Update specific records of a table based on search conditions</p>

A

<p>UPDATE Customers SET ContactName = 'Alfred Schmidt', City= 'Frankfurt' WHERE CustomerID = 1;</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

<p>What happens if I do this?UPDATE CustomersSET ContactName='Juan';</p>

A

<p>All entries will get updated regradless of any country , age , City etc. It is essential that while updating the correct filters are applied .Or it will corrupt entire data.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

<p>What happens if we execute the following code?</p>

<p>UPDATE Customers SET ContactName = 'Amos' AND Country = 'India' WHERE CustomerID = 1;</p>

A

<p>I will raise me an error , because for set compnonet we shouldnt use AND, instead we should use comma</p>

<p>UPDATE Customers SET ContactName = 'Amos',Country='India' WHERE CustomerID = 1;</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

<p>How to delete specific elements in table?</p>

A

<p>DELETE FROM table_name WHERE Name = "Sunny";</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

<p>What is one important warning to remember while deleting entries in SQL ?</p>

A

<p>We should careful regarding the condition we set in DELETE query DELETE FROM table_name WHERE condition;If we are not mindful of the condition then we might end up deleting the wrong entries.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

<p>How to delete all records from a certain table?</p>

A

<p>DELETE FROM table_name;</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

<p>What happens if we execute the follwing line?DELETE Age from Table where Name='Sunny';</p>

A

<p>It will raise me a syntax error .Because we cant delete a specific column from table . Deletion removes the complete entry ,not just a column.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

<p>How to update all entries in Table?</p>

A

<p>UPDATE CustomersSET ContactName = 'Alfred Schmidt';</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

<p>What happens if I exectue the following line?Update Student_Table where Name= "Sunny"</p>

A

<p>It will raise me an error because we havent specified the "set" component in update.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

<p>What are important components of update statement?</p>

A

<p>Udate table , SET values and condition</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

<p>What are important components of delete statement?</p>

A

<p>delete table and condition</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

<p>What happens if I excute the following code?</p>

<p>CREATE TABLE NewTable(Column INT AUTO_INCREMENT PRIMARY KEY);</p>

A

<p>It wont raise an error . The order of AUTO_INCREMENT and the key assignment doesent matter</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

<p>What happens if I execute the following line?Update Student_Table set Age = 20 where "Name" = Sunny</p>

A

<p>It will raise me an error because the field should not be a string and the value its checking (Sunny) should be a string.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

<p>How to get top 10 elements of a table</p>

A

<p>SELECT S.No,NameFROM StudentLIMIT 10;</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

<p>How to get bottom 10 element</p>

A

<p>SELECT S.No,NameFROM Student ORDER by S.No DESCLIMIT 10;</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

<p>What type of statistical attributes can we calculate from numerical fields in data table?</p>

A

<p>We can calclulate , COUNT , MIN , MAX, SUM AND AVG</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

<p>Format to calculate the statistical attributes of a table</p>

A

<p>Select MIN(Age) from Students where City = "Frankfurt"We can use the same for COUNT , MAX, SUM or AVG</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

<p>Get Elements where the name starts with letter 'a'</p>

A

<p>SELECT * FROM CustomersWHERE CustomerName LIKE 'a%';</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

<p>What key word should i use in case I want to find words that follow a certain pattern?</p>

A

<p>We should use the key words "LIKE"</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

<p>Get elements that end with letter 'a'</p>

A

<p>SELECT * FROM CustomersWHERE CustomerName LIKE '%a';</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

<p>What happens if I use the following syntax?SELECT * FROM CustomersWHERE CustomerName LIKE '*a';</p>

A

<p>This will raise me and error if I am using mySQL . But that wont be the case if we are using MS Access</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

<p>Get elements where the middle word is "or"</p>

A

<p>SELECT * FROM CustomersWHERE CustomerName LIKE '%or%';</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

<p>Get element that has second word 'r'</p>

A

<p>SELECT * FROM CustomersWHERE CustomerName LIKE '_r%';</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

<p>Get elements that has starting letter 'a' and has atleast 3 words in the string</p>

A

<p>SELECT * FROM CustomersWHERE CustomerName LIKE 'a\_\_%'; Note: There are 2 '_' in the syntax above</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

<p>Get elements that start with 'a' and end with 'o'</p>

A

<p>SELECT * FROM CustomersWHERE ContactName LIKE 'a%o';</p>

42
Q

<p>Get Customers with a CustomerName that does NOT start with "a"</p>

A

<p>SELECT * FROM CustomersWHERE CustomerName NOT LIKE 'a%';</p>

43
Q

<p>Get all customers with a City starting with "b", "s", or "p"</p>

A

<p>SELECT * FROM Customers WHERE CustomerName LIKE '[bsp]%';</p>

44
Q

<p>Get all customers with a City starting with "a", "b", "c","d"....till "p"</p>

A

<p>SELECT * FROM CustomersWHERE City LIKE '[a-p]%';</p>

45
Q

<p>Get all customers with a City NOT starting with "b", "s", or "p"</p>

A

<p>SELECT * FROM CustomersWHERE City LIKE '[!bsp]%';</p>

46
Q

<p>Get all customers that are located in "Germany", "France" or "UK"</p>

A

<p>SELECT * FROM CustomersWHERE Country ="Germany" OR Country ="France" OR Country ="UK"</p>

47
Q

<p>What is an alternative for getting customers from "Germany", "France" or "UK"</p>

A

<p>SELECT * FROM CustomersWHERE Country IN ('Germany', 'France', 'UK');</p>

48
Q

<p>Get all customers that are NOT located in "Germany", "France" or "UK"</p>

A

<p>SELECT * FROM CustomersWHERE Country NOT IN ('Germany', 'France', 'UK');</p>

49
Q

<p>Alternative for getting customers that are not located in "Germany", "France" or "UK"</p>

A

<p>SELECT * FROM CustomersWHERE NOT Country ="Germany" OR NOT Country ="France" OR NOT Country ="UK"</p>

50
Q

<p>Why do we use IN functionality when we could use OR logic or other logical expressions to get our results</p>

A

<p>Because we cant always cover ALL checking conditions . Sometimes there might be too many to cover ."IN" can be useful in situations like these.</p>

51
Q

<p>Get all customers that are from the same countries as the suppliers</p>

A

<p>SELECT * FROM Customers WHERE Country IN (SELECT Country FROM Suppliers); Note : If we used OR logical expressions in statements like these it would take forever.We might as well search manually</p>

<p>Also to be noted that we are using 2 tables here</p>

52
Q

<p>Get all products with a price between 10 and 20</p>

A

<p>SELECT * FROM ProductsWHERE Price BETWEEN 10 AND 20;</p>

53
Q

<p>Get all products outside a specific range like say less than 10 or greater than 20</p>

A

<p>SELECT * FROM ProductsWHERE Price NOT BETWEEN 10 AND 20;</p>

54
Q

<p>Get all products with a price between 10 and 20. AND In addition; do not show products with a CategoryID of 1,2, or 3:</p>

A

<p>SELECT * FROM ProductsWHERE Price BETWEEN 10 AND 20AND CategoryID NOT IN (1,2,3);</p>

55
Q

<p>What happens if I execute the following syntax?SELECT * FROM CustomersWHERE Country NOT IN ['Germany', 'France', 'UK'];</p>

A

<p>It raises me an error , Square brackets are not allowed .It should be ('Germany', 'France', 'UK');</p>

56
Q

<p>What happens if I try to arrange names in descending order example : Select name from student order by name DESC;</p>

A

<p>It will arrange in all names in opposite chronological order , Example : Z to A Manjimup , Lousiana , Herbert etc</p>

57
Q

<p>Get all products with a ProductName BETWEEN Carnarvon Tigers and Chef Anton's Cajun Seasoning</p>

A

<p>SELECT * FROM ProductsWHERE ProductName BETWEEN "Carnarvon Tigers" AND "Chef Anton's Cajun Seasoning";</p>

58
Q

<p>Get all products with a ProductName BETWEEN Carnarvon Tigers and Chef Anton's Cajun Seasoning WHILE maintaining chronological order</p>

A

<p>SELECT * FROM ProductsWHERE ProductName BETWEEN "Carnarvon Tigers" AND "Chef Anton's Cajun Seasoning" ORDER BY ProductName;</p>

59
Q

<p>Get all products with a ProductName NOT BETWEEN Carnarvon Tigers and Mozzarella di Giovanni</p>

A

<p>SELECT * FROM ProductsWHERE ProductName NOT BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di Giovanni';</p>

60
Q

<p>How are primary key and unique key similar?</p>

A

<p>A primary key is by default a unqiue key but a unique key is not primary key .</p>

61
Q

<p>What is an important charecteristc of primary key?</p>

A

<p>A primary key is a key that is assigned to a column and identifies each record of a table unqiuely , that said there CAN BE only one primary key .</p>

62
Q

<p>What happens if I execute this?</p>

<p>CREATE TABLE newTable4(col1 INT AUTO_INCREMENT);</p>

A

<p>It will raise me an error , because you CANT assign AUTO_INCREMENT without assining key to that column.</p>

63
Q

<p>What happens if I execute the following?</p>

<p>CREATE TABLE newTable4(col1 UNIQUE KEY AUTO_INCREMENT INT);</p>

A

<p>It will raise an error because you need to mention the type of the field first before you can assign tokens such as UNQIUE KEY or AUTO_INCREMENT</p>

64
Q

<p>What happens if I execute the following code?</p>

<p>SELECT DISTINCT(COL1) FROM table_name</p>

A

<p>I tiwll raise me an error.You cant use "()"</p>

<p>Correct syntax :</p>

<p>SELECT DISTINCT COL1 FROM table_name</p>

65
Q

<p>What happens if I execute the following code?</p>

<p>CREATE TABLE table_name(AUTO_INCREMENT PRIMARY KEY)</p>

A

<p>It will raise and error because we havent specified column name</p>

66
Q

<p>What happens if I excute the follwing code ?</p>

<p>CREATE TABLE table_name (col_name INT, PRIMARY KEY , AUTOINCERMENT)</p>

A

<p>I will raise an error because of ',' in between . There should be only spaces</p>

67
Q

<p>Line to read a CSV file</p>

A

<p>import pandas as pd</p>

<p>df=pd.read_csv("k.csv")</p>

68
Q

<p>What does df.head(3) do ?</p>

A

<p>Prints the first 3 entires</p>

69
Q

<p>What does df.tail(3) do ?</p>

A

<p>Fetches the last 3 entries of all entries</p>

70
Q

<p>What is the line to read excel sheets</p>

A

<p>excel_data_df = pandas.read_excel('records.xlsx', sheet_name='Employees')</p>

71
Q

<p>How do you read a file that ISNT CSV but has different delimitter? 8:18</p>

A

<p>df = pd.read_csv("pk_data.txt".delimiter='\t' )</p>

<p></p>

72
Q

<p>Line to get names of all columns in the data file</p>

A

<p>df.columns</p>

73
Q

<p>Line to print one certain column</p>

A

<p>dt['Col_Name']</p>

74
Q

<p>Line to get specific column of top 5 entries</p>

A

<p>dt['Col'][0:5]</p>

75
Q

<p>Line to get multple selective columns</p>

A

<p>dt[['Col1','Col2']]</p>

76
Q

<p>Line to get all details concerning row at certain position</p>

A

<p>df.iloc[1]</p>

<p>(Getting entry at position 2 zero indexing applies)</p>

77
Q

<p>Line to get all details concerning multiple first few row</p>

A

<p>df.iloc[0:2]</p>

78
Q

<p>Line to get a specific column OF a certain entry using ONLY INDICES</p>

A

<p>df.iloc[1,2]</p>

<p>1 is the second entry</p>

<p>And 2 stands for the 3rd column (We have to account for the zero indexing)</p>

79
Q

<p>Line to iterate through all entries of datasheet</p>

A

<p>for index,df in df.iterrows():<br></br>print(index,df)</p>

<p>/////df stands for data frame/////</p>

80
Q

<p>Line to iterate through rows and print specific columns of those entries</p>

A

<p>for index,df in df.iterrows():<br></br>print(index,df['Name'])</p>

81
Q

<p>Line to to fetch data entries that satisfy a certain condition(Search filters).</p>

A

<p>df.loc[df['Type 1'] == Grass]</p>

<p>df.loc['Type 1'] returns all data strings under Type 1 column and then the condition checks which of them are 'Grass'</p>

82
Q

<p>Line to fetch data that has multiple filter layers</p>

A

<p>df.loc[df['Type 1'] == Grass].loc[df['Type 1'] == Fire]]</p>

83
Q

<p>Line to get standard deviation of a certain column</p>

A

<p>df['Age'].describe()['std']</p>

84
Q

<p>What is the use of describe() is pandas</p>

A

<p>It displays data like count ,mean , min , std, 25percent, 75 and max</p>

85
Q

<p>Sort values in ascending order</p>

A

<p>df.sort_values('Power')</p>

86
Q

<p>Sort names in leographic order</p>

A

<p>df.sort_values('Name')</p>

87
Q

<p>Sort elements in descending order</p>

A

<p>df.sort_values('Name',ascending = False)</p>

88
Q

<p>Sort first column with ascending and second column with desc</p>

A

<p>df.sort_values(['Type1','HP'],ascending=[1,0])<br></br></p>

89
Q

<p>Line to Create new column which is function of previous columns</p>

A

<p>df['Total'] = df['HP']+df['Attack']-df['Damage']</p>

<p>/////It automatically creates a new column for the rest ////</p>

90
Q

<p>Remove muliple columns</p>

A

<p>df = df.drop(columns = ['Total1','Total2'])</p>

91
Q

<p>Getting stats for specific column</p>

A

<p>df["Age"].describe()</p>

92
Q

<p>What does descibe function of pandas dataframe return me ?</p>

A

<p>It returns me a dictionary of various quantites</p>

93
Q

<p>Line to get a specific column (VIA LABEL) OF a certain entry (VIA INDEX)</p>

A

<p>first = data.iloc[0]['Age']</p>

<p>We used 0 as index for entry (Meaning first row ) and label as identifier for specific column (Age is the column)</p>

94
Q

<p>Line to get standard deviation of all columns</p>

A

<p>df.describe()['std']</p>

95
Q

<p>Error debugging #1</p>

<p>What happens if you write print(df[Name])</p>

A

<p>It will raise an error because in df[Name] we should pass a stiring, not just Name(Which might work if it was defined as a string var)</p>

96
Q

<p>What is the difference between iloc and loc functions ?</p>

A

<p>loc is used for fetching entires based on boolean conditions</p>

<p>whereas</p>

<p>iloc is used for fetching results based on simple indices.</p>

<p>(Hence the use of i, which probably means index locator)</p>

97
Q

<p>Line for filtering data of a certain column using indexof column BUT NOT LABEL<br></br><br></br></p>

A

<p>df.loc[df.iloc[:,6]</p>

98
Q

<p>Line for printing specific entires based on comparision filters</p>

A

<p>df.loc[df['Col1']</p>

99
Q

<p>Line for printing specific columns AFTER performing a search criteria</p>

A

<p>df.loc[df['Units']</p>

100
Q

<p>Line to replace all entries that contain specific element(string or value) with a DIFFERENT element</p>

A

<p>df.replace(to_replace ="Boston Celtics",value ="Omega Warrior")</p>

101
Q

<p>Line to replace all entries that contain specific element (string or value) that belong to a set with a DIFFERENT element</p>

A

<p>df.replace(to_replace =["Boston Celtics", "Texas"], value ="Omega Warrior")</p>