{"id":18982542,"url":"https://github.com/rtahmasbi/sql","last_synced_at":"2025-09-01T09:35:19.852Z","repository":{"id":201466744,"uuid":"163472400","full_name":"rtahmasbi/SQL","owner":"rtahmasbi","description":null,"archived":false,"fork":false,"pushed_at":"2018-12-29T05:27:24.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-21T09:32:34.097Z","etag":null,"topics":["sql"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rtahmasbi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-12-29T03:32:43.000Z","updated_at":"2018-12-29T05:27:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"c0e51cb5-71fa-40e5-9990-f1907ef0e503","html_url":"https://github.com/rtahmasbi/SQL","commit_stats":null,"previous_names":["rtahmasbi/sql"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/rtahmasbi/SQL","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtahmasbi%2FSQL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtahmasbi%2FSQL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtahmasbi%2FSQL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtahmasbi%2FSQL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rtahmasbi","download_url":"https://codeload.github.com/rtahmasbi/SQL/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rtahmasbi%2FSQL/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273100429,"owners_count":25045697,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-01T02:00:09.058Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["sql"],"created_at":"2024-11-08T16:13:52.723Z","updated_at":"2025-09-01T09:35:19.830Z","avatar_url":"https://github.com/rtahmasbi.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\n`ctrl+shift+m` for preview in atom\n\n\n### Example from w3schools\nFrom https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all\n```\nCustomers : CustomerID, CustomerName, ContactName, Address, City, PostalCode, Country\nOrders: OrderID, CustomerID, EmployeeID, OrderDate, ShipperID\n```\n\n\n\n```\nSELECT Country, COUNT(Country) AS C FROM (SELECT Customers.CustomerID, Customers.Country, Orders.ShipperID\n    FROM Customers JOIN Orders ON Orders.CustomerID=Customers.CustomerID) AS temp GROUP BY Country ORDER BY C DESC;\n```\n\n\n\n\n```\nSELECT COUNT(DISTINCT) FROM (SELECT Customers.CustomerID\nFROM Orders\nJOIN Customers\nON Orders.CustomerID=Customers.CustomerID)\n```\n\n\n```\nSELECT Country, City, COUNT(*) FROM (SELECT Customers.CustomerID, Customers.Country, Customers.City\nFROM Orders\nJOIN Customers\nON Orders.CustomerID=Customers.CustomerID) AS temp GROUP BY Country,City ORDER BY Country ASC, City DESC;\n```\n\n\n```\nORDER BY column1, column2, ... ASC|DESC;\n\nORDER BY Country ASC , City DESC;\n```\n\n\n```\nSELECT SS, COUNT(*) as aaa FROM (SELECT Customers.CustomerID, Customers.Country AS SS\nFROM Orders\nJOIN Customers\nON Orders.CustomerID=Customers.CustomerID)\nGROUP BY SS\nORDER BY aaa DESC, SS ASC;\n```\n\n\n\n```\nSELECT DEPARTMENT.NAME, COUNT(*) AS COUNT_OF_STUDENTS_IN_THE_DEPARTMENT FROM (SELECT STUDENT.DEPT_ID, DEPARTMENT.ID, DEPARTMENT.NAME\nFROM STUDENT\nJOIN DEPARTMENT\nON STUDENT.DEPT_ID=DEPARTMENT.ID)\nGROUP BY DEPARTMENT.NAME\nORDER BY COUNT_OF_STUDENTS_IN_THE_DEPARTMENT DESC, DEPARTMENT.NAME ASC;\n```\n\n\n\n\n\n\n\n### Write a SQL query to get the third highest salary of an employee from employee_table?\n\n```\nSELECT TOP 1 salary\nFROM(\nSELECT TOP 3 salary\nFROM employee_table\nORDER BY salary DESC) AS temp\nORDER BY salary ASC;\n\n```\n\n\n### ROLL_NO BETWEEN or IN\n```\nSELECT * FROM Students where ROLL_NO BETWEEN 10 AND 50;\nSELECT * FROM students where ROLL_NO IN (8,15,25);\n```\n\n\n\n### What are the different set operators available in SQL?\nSome of the available set operators are – Union, Intersect or Minus operators.\n\n```\nSelect DISTINCT studentID from Student\n```\n\n\n### SUBSTRING\nHow can you fetch first 5 characters of the string?\nThere are a lot of ways to fetch characters from a string. For example:\n```\nSelect SUBSTRING(StudentName,1,5) as studentname from student\n```\n\n\n### LIKE\n```\nSELECT * FROM Employees WHERE EmpName like 'A%' ;\n```\n\n\n### CREATE\n```\nCREATE TABLE Orders\n(\nO_ID int NOT NULL,\nORDER_NO int NOT NULL,\nC_ID int,\nPRIMARY KEY (O_ID),\nFOREIGN KEY (C_ID) REFERENCES Customers(C_ID)\n)\n```\n\n\n### DELETE\nDelete duplicate data from table only first data remains constant.\n```\nDELETE M1 From managers M1, managers M2\nWhere M2.Name = M1.Name AND M1.Id\u003eM2.Id;\n```\n\n\n### INSERT INTO\n```\nINSERT INTO table_name (column1, column2, column3, ...)\nVALUES (value1, value2, value3, ...);\n```\n\n\n\n\n### COALESCE\nFinding the name of Employees where First Name, Second Name and Last Name is given in table. Some Name is missing such as First Name, Second Name and may be Last Name. Here we will use COALESCE() function which will return first Non Null values.\n\n```\nSELECT ID, COALESCE(FName, SName, LName) as Name\nFROM employees;\n```\n\n\n### TIMESTAMPDIFF\nFind the Employees who hired in the Last n months.\n\n```\nelect *, TIMESTAMPDIFF (month, Hiredate, current_date()) as DiffMonth\nFrom employees\nWhere TIMESTAMPDIFF (month, Hiredate, current_date())\nBetween 1 and 5 Order by Hiredate desc;\n```\n\n\n### DATEDIFF\nFind the Employees who hired in the Last n days.\n```\nSELECT *, DATEDIFF (current_date(), Hiredate) as DiffDay\nFrom employees\nWhere DATEDIFF (current_date(), Hiredate) between 1 and 100 order by Hiredate desc;\n```\n\n\n### LEFT, like, substring\n```\nSELECT * From employees Where Fname like 'A%';\nSELECT * From employees Where left(FName, 1)='A';\nSELECT * From employees Where substring(FName, 1, 1)='A';\n```\n\n\n\n\n\n\n### SELECT INTO\n```\nSELECT * INTO newtable FROM oldtable WHERE condition;\n```\n\n\n\n### UNION and UNION ALL\n```\nSELECT * FROM Table1\nUNION\nSELECT * FROM Table2\n```\n\n\nkeeping all the duplicates:\n\n```\nSELECT * FROM Table1\nUNION ALL\nSELECT * FROM Table2\n```\n\n\n\n## ALTER - Add a column\nHow to add a column ‘Salary’ to a table Employee_Details?\n```\nALTER TABLE Employee_Details ADD (Salary);\n```\n\n\n### List of All Tables From A DataBase\n\nTo view the tables available on a particular DataBase\n```\nUSE TestDB\nGO\nSELECT * FROM sys.Tables\nGO\n```\n\n### EXCEPT\nHow to fetch values from TestTable1 that are not in TestTable2 without using NOT keyword?\n```\nSELECT * FROM TestTable1 EXCEPT SELECT * FROM TestTable2;\n```\n\n\n\n\n### What is the order of SQL SELECT?\nOrder of SQL SELECT statement is as follows\n\n```\nSELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.\n```\n\n\n\n\n### How to display the current date in SQL?\nIn SQL, there is a built-in function called GetDate() which helps to return the current date.\n```\nSELECT GetDate();\n```\n\n\n\n### To select all the even number records from a table:\n```\nSelect * from table where id % 2 = 0\n```\n\n\n### CASE\nCan you display the result from the below table TestTable based on the criteria M,m as M and F, f as F and Null as N and g, k, I as U\n\n```\nSELECT Gender,\ncase\nwhen Gender='i' then 'U'\nwhen Gender='g' then 'U'\nwhen Gender='H' then 'U'\nwhen Gender='NULL' then 'N'\nelse upper(Gender)\nend as newgender from TestTable GROUP BY Gender\n```\n\n\n\n### CASE\nHow do you update F as M and M as F from the below table TestTable?\n```\nUPDATE TestTable SET Gender = CASE Gender WHEN 'F' THEN 'M' ELSE 'F' END\n```\n\n\n\n\n\n### Mathematics\n```\nSELECT col1 * (col2 + IFNULL(col3,0)) FROM Table1\n```\n\n\n\n#\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frtahmasbi%2Fsql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frtahmasbi%2Fsql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frtahmasbi%2Fsql/lists"}