{"id":19218709,"url":"https://github.com/fadilxcoder/sql-query","last_synced_at":"2025-08-07T05:10:25.889Z","repository":{"id":86245762,"uuid":"349806322","full_name":"fadilxcoder/sql-query","owner":"fadilxcoder","description":"SQL query explained","archived":false,"fork":false,"pushed_at":"2022-08-18T17:28:00.000Z","size":59,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-23T09:14:44.848Z","etag":null,"topics":["notes","php","poc","sql"],"latest_commit_sha":null,"homepage":"","language":"PHP","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/fadilxcoder.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-20T18:33:35.000Z","updated_at":"2022-07-06T18:15:13.000Z","dependencies_parsed_at":"2023-03-08T10:00:37.575Z","dependency_job_id":null,"html_url":"https://github.com/fadilxcoder/sql-query","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fadilxcoder/sql-query","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fadilxcoder%2Fsql-query","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fadilxcoder%2Fsql-query/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fadilxcoder%2Fsql-query/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fadilxcoder%2Fsql-query/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fadilxcoder","download_url":"https://codeload.github.com/fadilxcoder/sql-query/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fadilxcoder%2Fsql-query/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269201296,"owners_count":24377460,"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-08-07T02:00:09.698Z","response_time":73,"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":["notes","php","poc","sql"],"created_at":"2024-11-09T14:27:49.543Z","updated_at":"2025-08-07T05:10:25.861Z","avatar_url":"https://github.com/fadilxcoder.png","language":"PHP","readme":"# NOTES\n\n## DB \n\n- *users* 1 - N *cards*\n- *users* 1 - N *addresses*\n\n- Update table with random integer : `UPDATE tbl_name SET column_name = CAST(RAND() * 10000\u003cany range on number\u003e AS UNSIGNED)`\n\n ## JOINS\n\n\u003cimg src=\"./assets/join-illustration.jpg\"/\u003e\n\n### Information\n\n- FULL JOIN === FULL OUTER JOIN *Does not work in MySQL*\n- LEFT OUTER JOIN === LEFT JOIN\n- RIGHT OUTER JOIN === RIGHT JOIN\n- INNER JOIN === JOIN\n\u003cbr\u003e\n\n**Above illustration A is` users`, B is `cards`**\n\u003cbr\u003e\n\n\u003e SELECT U.id, U.fname, U.lname, C.type, C.amount\nFROM `users` AS U \nINNER JOIN `cards` AS C \nWHERE C.uid = U.id\n\n*Return only matched - 300,000 results*\n\u003cbr\u003e\n\n\u003e SELECT U.id, U.fname, U.lname, C.type, C.amount\nFROM `users` AS U \nLEFT JOIN `cards` AS C \nON C.uid = U.id\n\n*Return matched \u0026 all in users - 2,969,158 results*\n\u003cbr\u003e\n\n\u003e SELECT U.id, U.fname, U.lname, C.type, C.amount\nFROM `users` AS U \nRIGHT JOIN `cards` AS C \nON C.uid = U.id\n\n*Return matched \u0026 all in cards - 300,000 results*\n\u003cbr\u003e\n\n\u003e SELECT *\nFROM users\nCROSS JOIN cards\n\n*Return combinations of each row of users \u003c first_table \u003e with **all** records in cards \u003c second_table \u003e - 886,334,700,000 results*\n\u003cbr\u003e\n\n## HAVING / GROUP BY / UNION\n\n### Reference\n\n- The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.\n- https://www.w3schools.com/sql/sql_groupby.asp\n\n\u003e SELECT U.id, U.fname, U.lname, C.amount\nFROM `users` AS U \nINNER JOIN `cards` AS C \nON C.uid = U.id\nHAVING U.id \u003c 100\n\n*Return only matched \u0026 all user.id \u003c 100 - 14 results*\n\u003cbr\u003e\n\n\u003e SELECT sum(amount), type\nFROM cards\nGROUP BY type\nORDER BY type ASC\n\n*Return 5 type of card with sum of amount per card type - 5 results*\n\u003cbr\u003e\n\n\u003e SELECT concat('C', C.id) AS id, C.uid, 'Cards' AS status\nFROM cards AS C\nUNION\nSELECT concat('A', A.id) AS id, A.uid, 'Addresses' AS status\nFROM addresses AS A\n\n*Return all of cards \u0026 all of addresses, **note that columns need to be of the same type** - 598,862  results*\n\u003cbr\u003e\n\n## VIEW\n\n\u003e CREATE VIEW view_1 AS\nSELECT U.id, U.fname, U.lname, C.amount\nFROM `users` AS U \nINNER JOIN `cards` AS C \nON C.uid = U.id\nHAVING U.id \u003c 100\n\n*Will create view_1 in phpMyAdmin listed under Structure with type:**View***\n\u003cbr\u003e\n\n---\n\n# Additional Notes\n\n- https://dev.mysql.com/doc/refman/8.0/en/json-creation-functions.html ( Functions That Create JSON Values)\n\n```sql\nSELECT \nvip_list.id AS id,\nvip_list.name AS name,\nvip_list.surname AS surname,\nJSON_OBJECT(\n    'qa', vip_list.qa,\n    'status', vip_list.status,\n    'net_worth', vip_list.net_worth,\n    'registered_at', vip_list.registered_at\n) AS details\nfrom vip_list\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffadilxcoder%2Fsql-query","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffadilxcoder%2Fsql-query","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffadilxcoder%2Fsql-query/lists"}